0

possible duplicates

ListView OnItemClickListener Not Responding?

OnItemClickListener and OnClickListener not working for ListView

I have added onClickListener to root layout of list item and according to the answer to above question and this post I made same root layout focusable=false. Tried from both, java as well as xml.

But still no luck, OnItemClickListener is not responding to item click events.

please have a look at following code,

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/todoly_selector_list_item">
    
    [..Some code..]

</RelativeLayout>

and the getView of adapter has following code,

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.list_item, null);
    
    /*
     * I need to have this, if I remove this line OnItemClickListener works,
     * no otherwise.
     */ 
    convertView.setOnClickListener(onClickListener);
    
    convertView.setFocusable(false);
    
    [..Some code..]

}

What I am doing wrong here? or Is it not possible to do so?

Community
  • 1
  • 1
Rupesh
  • 3,415
  • 2
  • 26
  • 30
  • Perhaps it'd be good to explain *why* you're saying you need an `OnClickListener` on the list item's root? That's effectively what an `OnItemClickListener` is for, so why would you need both? – MH. May 16 '13 at 19:07
  • I am wrinting a multi select adapter as in [this](http://dharmendra4android.blogspot.in/2012/10/multi-selection-listview-example-in.html) post. Addition to that is, I have a `mIsMultiSelect` attribute which can be used for toggling the multiselect mode. multiselect will starts on item long press and selection will be based on item click. but as the problem, when I add `OnClickListener` to root layout of item then `OnItemClickListener` as well as `OnItemLongClickListener` stops responding.. – Rupesh May 16 '13 at 19:20
  • In that case, you should set the mode in the `OnItemLongClickListener` and then based on that decide what should happen in `OnItemClickListener` - you shouldn't have to need another `OnItemClickListener` on the row items for that. – MH. May 16 '13 at 19:31
  • In that way the selection logic will not go in the `Adapter`, I just made it work, `getView` of adapter is now having this code. `if (mIsMultiSelect) { convertView.setClickable(true); convertView.setOnClickListener(onClickListener); } else { convertView.setSelected(false); convertView.setClickable(false); }` and `onClick` is `public void onClick(View v) { if (mIsMultiSelect) { v.setSelected(!v.isSelected()); } }` – Rupesh May 16 '13 at 20:08

0 Answers0