1

I am using a Custom CursorAdapter that overrides newView() and bindView() to show a custom ListView.

I am inflating the row layout xml file and binding a OnClickListener to the view passed as argument to bindView():

public void bindView(View view, Context context, Cursor cursor) {
view.setOnClickListener(new OnClickListener() { }
...
}

I am also using a relative layout in the row xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
      android:paddingTop="10px"
    android:paddingBottom="10px"
    android:paddingLeft="10px"
    android:paddingRight="10px"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
  >

    <TextView android:id="@+id/episode_title_text" xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
    android:textSize="20px"
    android:layout_weight="1" 
    />

</RelativeLayout>

My problem is when I click the items in the list, nothing happens, OnClickListener.onClick() is never called.

Any ideas on what the problem might be?

Thank you.

simao
  • 14,491
  • 9
  • 55
  • 66

3 Answers3

4

I used descendantFocusability="blocksDescendants in my RelativeLayout and it worked.

I think this works because the TextView is not able to get focus so the List row gets focus and can respond to onItemClickListener.

I got this from the comments at How to fire onListItemClick in Listactivity with buttons in list?

If anyone can explain this better than me I'll accept that answer.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
simao
  • 14,491
  • 9
  • 55
  • 66
1

Instead of setting an OnClickListener to your row, use setOnItemClickListener(OnItemClickListener) in your ListView.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • I used `getListView().setOnItemClickListener(new OnItemClickListener() {` in my onCreate() but still doesn't work.. – simao Jan 24 '11 at 23:50
0

I found that you need to do:

android:focusable="false"

for each item you wish to be clickable in your custom row view (for example I put this in imPlay, llFirstHalf,llSecondHalf, and cbCheck). Create onClick listeners for the areas you wish to be clickable inside that view. My example made 4 clickable areas in my listview row view.

@Override
public void bindView(View v, Context context, Cursor c) {

    ImageView imPlay = (ImageView) v.findViewById(R.id.row_play_button);
    LinearLayout llFirstHalf = (LinearLayout) v.findViewById(R.id.row_first_half);
    LinearLayout llSecondHalf = (LinearLayout) v.findViewById(R.id.row_second_half);
    CheckBox cbCheck = (CheckBox) v.findViewById(R.id.row_checkbox);

    imPlay.setOnClickListener(this);
    llFirstHalf.setOnClickListener(this);
    llSecondHalf.setOnClickListener(this);
    cbCheck.setOnClickListener(this);



}

@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.row_checkbox:
        Log.e("ListAdapter onClick","checkbox");
        break;
    case R.id.row_first_half:
        Log.e("ListAdapter onClick","row first half");
        break;
    case R.id.row_second_half:
        Log.e("ListAdapter onClick","row second half");
        break;
    case R.id.row_play_button:
        Log.e("ListAdapter onClick","play button");
        break;
    }

}
Jordan Hochstetler
  • 1,308
  • 3
  • 17
  • 28