Search code examples
androidlistviewclicksimplecursoradapter

ListView's click listener does not work


I have ListView with custom SimpleCursorAdapter in Activity:

public class MyListActivity extends Activity { ... }

mCursorAdapter = new MyCursorAdapter(mContext,
     R.layout.listview_item01, mCursor, from, to);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);

mListView = new ListView(mContext);
mListView.setAdapter(mCursorAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> arg0, View view,
          int position, long arg3) {
          Toast.makeText(getApplicationContext(),
               "You Selected Item " + Integer.toString(position),
               Toast.LENGTH_LONG).show();
          Log.v("onItemClick", "CLICK!");
    }
});
mainLayout.addView(mListView);

My simple adapter:

private class MyCursorAdapter extends SimpleCursorAdapter {

     public MyCursorAdapter(Context context, int layout, Cursor c,
          String[] from, int[] to) {
          super(context, layout, c, from, to);
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
          View view = super.getView(position, convertView, parent);
          if (position % 2 == 0) {
               view.setBackgroundColor(Color.rgb(238, 233, 233));
          } else {
               view.setBackgroundColor(Color.rgb(255, 255, 255));
          }
          return view;
     }
}

My list view item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RadioButton
        android:id="@+id/item_radiobutton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
    <TextView
        android:id="@+id/item_textview01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/item_radiobutton01"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/item_radiobutton01"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

What I see:

enter image description here

My ListView looks fine, but if I click any item so nothing happines. Why? Help me please


Solution

  • I think that this is because of your layout items. Try to disable the clickability of them using these:

        android:focusable="false"
        android:focusableInTouchMode="false"
    

    I think when you clicking on an item you do not actually click on the row but you are clicking on the TextViewor the RadioButton; you can check this by writing event handlers for them too and put a Log inside those event handlers.

    For more information about these properties take a look at this document in android developers website.