setOnItemClickListener() not working on custom ListView @ Android-ThrowExceptions

Exception or error:

I have Implemented a custom ListView by extending LinearLayout for every row. Every row has a small thumbnail, a text and a check box. The list view is deployed properly and I can scroll and fling through it without any problems.

But The ListView doesn’t seem to respond to the setOnItemClickListener() at all, So I had to find a workaround by setting click listener in the getView() of the Text inside every row which is obviously creating problem when I am trying to reuse the adapter. Does anyone have a solution?

How to solve:

Try this
For ListView,

final ListView list = (ListView) findViewById(R.id.list);
list.setItemsCanFocus(false);

Also, make sure that for CheckBox inside list item set focusable false

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

###

old answer: I wrote in previous post here

android:focusable="false"
android:clickable="false"

will not help when ImageButton is in custom view.. One must use button.setFocusable(false); during runtime (from java source code)

Edit: There is even more elegant solution. Try to add android:descendantFocusability="blocksDescendants" in root layout of list element. That will make clicks onListItem possible and separately u can handle Button or ImageButton clicks

###

For a ListView where you set the item views to CheckBox

android:focusable="false"
android:clickable="false"

http://code.google.com/p/android/issues/detail?id=3414

###

Set these properties:

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

for your all UI elements in your list_item.xml file.

if this is not resolved in your adapter set:

    v.imageView.setFocusable(false);
    v.imageView.setFocusableInTouchMode(false);

###

Did you made any ViewHolder in your extended adapter class? If yes, then make an instance of your that placeholder in the setOnItemClickListener() something will may work like this.

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {        
        View rowView = v;
        if (rowView == null) {
            LayoutInflater inflater = this.getLayoutInflater();
                // GET INFLATE OF YOUR LAYOUT.
            rowView = inflater.inflate(R.layout.projectpeopledescrate, null);
             // CUSTOM ViewHolder Class Created in Adapter.
// name,title,comment are my components on the same listview clicked item.
            PPDViewHolder viewHolder = new PPDViewHolder();
            viewHolder.name     = (TextView) rowView.findViewById(R.id.ppeopledescrvname);
            viewHolder.title    = (TextView) rowView.findViewById(R.id.ppeopledescrvtime);
            viewHolder.comment  = (TextView) rowView.findViewById(R.id.ppeoplervcomment);
            viewHolder.hiddenLayout = (RelativeLayout) rowView.findViewById(R.id.hiddenCommentPanel); 
            rowView.setTag(viewHolder);
        }
          // ANOTHER object instance to apply new changes.
        PPDViewHolder holder = (PPDViewHolder) rowView.getTag();
// I've setted up visibility over the components. You can set your onClickListener over your buttons.
        holder.comment.setVisibility(View.GONE);
        holder.name.setVisibility(View.GONE);
        holder.title.setVisibility(View.GONE);
        holder.hiddenLayout.setVisibility(View.VISIBLE);
        holder.hiddenLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
        holder.hiddenLayout.bringToFront();

    }

Hope, you want something same. Good Luck!

###

I have this code

this.mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.v(TAG,"loul");
            }
        });

But it didn’t work

So i have juste put a onItemSelectedListener under and it work Oo :

this.mListView.setItemsCanFocus(false);
this.mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.v(TAG,"loul");
    }
});
//listener for nothing but it allow OnItemClickListener to work
this.mListView.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *