Note that there are some explanatory texts on larger screens.

plurals
  1. POonItemClickListener not firing on custom ArrayAdapter
    text
    copied!<p>I have an <code>Activity</code> that retrieves data from a web service. This data is presented in a <code>ListView</code> via an ArrayAdapter which inflates a <code>RelativeLayout</code> with three <code>TextViews</code> inside, nothing fancy and it work fine.</p> <p>Now I want to implement a Details <code>Activity</code> that should be called when a user clicks an item in the ListView, sounds easy but I can't for the life of me get the onItemClickListener to work on my ArrayAdapter.</p> <p>This is my main <code>Activity</code>:</p> <pre><code>public class Schema extends Activity { private ArrayList&lt;Lesson&gt; lessons = new ArrayList&lt;Lesson&gt;(); private static final String TAG = "Schema"; ListView lstLessons; Integer lessonId; // called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // can we use the custom titlebar? requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // set the view setContentView(R.layout.main); // set the title getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); // listview called lstLessons lstLessons = (ListView)findViewById(R.id.lstLessons); // load the schema new loadSchema().execute(); // set the click listeners lstLessons.setOnItemClickListener(selectLesson); }// onCreate // declare an OnItemClickListener for the AdapterArray (this doesn't work) private OnItemClickListener selectLesson = new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View v, int i, long l) { Log.v(TAG, "onItemClick fired!"); } }; private class loadSchema extends AsyncTask&lt;Void, Void, Void&gt; { private ProgressDialog progressDialog; // ui calling possible protected void onPreExecute() { progressDialog = ProgressDialog.show(Schema.this,"", "Please wait...", true); } // no ui from this one @Override protected Void doInBackground(Void... arg0) { // get some JSON, this works fine } @Override protected void onPostExecute(Void result) { progressDialog.dismiss(); // apply to list adapter lstLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons)); } </code></pre> <p>My ArrayAdapter code:</p> <pre><code>// custom ArrayAdapter for Lessons private class LessonListAdapter extends ArrayAdapter&lt;Lesson&gt; { private ArrayList&lt;Lesson&gt; lessons; public LessonListAdapter(Context context, int textViewResourceId, ArrayList&lt;Lesson&gt; items) { super(context, textViewResourceId, items); this.lessons = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.list_item, null); } Lesson o = lessons.get(position); TextView tt = (TextView) v.findViewById(R.id.titletext); TextView bt = (TextView) v.findViewById(R.id.timestarttext); TextView rt = (TextView) v.findViewById(R.id.roomtext); v.setClickable(true); v.setFocusable(true); tt.setText(o.title); bt.setText(o.fmt_time_start); rt.setText(o.room); return v; } }// LessonListAdapter </code></pre> <p>The main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout android:id="@+id/main" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:screenOrientation="portrait" &gt; &lt;!-- student name --&gt; &lt;TextView android:id="@+id/schema_view_student" android:text="Name" android:padding="4dip" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center_vertical|center_horizontal" style="@style/schema_view_student" /&gt; &lt;!-- date for schema --&gt; &lt;TextView android:id="@+id/schema_view_title" android:layout_height="wrap_content" android:layout_margin="0dip" style="@style/schema_view_day" android:gravity="center_vertical|center_horizontal" android:layout_below="@+id/schema_view_student" android:text="Date" android:padding="6dip" android:layout_width="fill_parent" /&gt; &lt;!-- horizontal line --&gt; &lt;View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#55000000" android:layout_below="@+id/schema_view_title" /&gt; &lt;!-- list of lessons --&gt; &lt;ListView android:id="@+id/lstLessons" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/schema_view_title" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The list_item.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60px" android:padding="12dip"&gt; &lt;TextView android:id="@+id/timestarttext" android:text="09:45" style="@style/LessonTimeStartText" android:layout_width="60dip" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_height="fill_parent" android:gravity="center_vertical|right" android:paddingRight="6dip"/&gt; &lt;TextView android:id="@+id/titletext" android:text="Test" style="@style/LessonTitleText" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@+id/timestarttext" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:gravity="center_vertical|center_horizontal"/&gt; &lt;TextView android:id="@+id/roomtext" android:text="123" android:layout_width="wrap_content" android:layout_height="fill_parent" style="@style/LessonRoomText" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:gravity="center_vertical" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Been messing with this for the last couple of hours and I can't seem to get my head around what the problem is. My problem looks very similar to <a href="https://stackoverflow.com/questions/5066220/get-specific-item-property-on-onlistitemclick-using-custom-arrayadapter">this question</a>, but I'm not extending ListActivity, so I still don't know where my onListClickItem() should go.</p> <p><strong>UPDATE:</strong> Now I've puzzled with this for several days and still can't find the issue. </p> <p>Should I rewrite the activity, this time extending ListActivity instead of Activity? Because it provides the onItemClick method itself and is probably easier to overwrite.</p> <p>Or, should I bind a listener directly in each getView() in my ArrayAdapter? I believe I have read this is bad practice (I should do as I tried and failed in my post).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload