1

I have really searched a lot on google, stack overflow , and found this : OnItemCLickListener not working in listview ANDROID. But seems sunshine's answer not works for my case. Other answers are all similar ones.

I have tried the following approaches:

add android:focusable="false" to my list item xml

add TextView.setFocusable(false) and TextView.setClickable(false) in ViewHolder

using the xml as described in the above link.

But none of them work.

Here's my xml and java code:

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:paddingTop="2dp"
android:gravity="center_vertical" >

<TextView
    android:id="@+id/ninegrid_number_list_choice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/ninegrid_number_listchoice_text_size"
    android:gravity="center"
    >
</TextView>
</LinearLayout>

getView int list adapter.java:

@Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null ) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.mTextView = (TextView)convertView.findViewById(R.id.ninegrid_number_list_choice);
            holder.mTextView.setFocusable(false);
            holder.mTextView.setClickable(false);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.mTextView.setText(mList.get(position));
        holder.mTextView.setTextColor(mTextColor);
        holder.mTextView.setFocusable(false);
        holder.mTextView.setClickable(false);
        return convertView;
    }

Edit: in my activity:

 listchoice.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
 listChoice.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {

      Log.v(tag, "sdf");
 }
 });
Community
  • 1
  • 1
suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • 1
    You aren't showing us the code where you register the onItemClick listener. Are you registering the listener properly? – Nick Palmer Feb 11 '14 at 03:10
  • where are you setting the onItemClickListener(), show that code !! – amit singh Feb 11 '14 at 03:10
  • I've never used `setClickable(false)` or `setFocusable(false)` on the subviews in the `getView` method. Maybe that's causing issues? – Tenfour04 Feb 11 '14 at 03:14
  • @Tenfour04, there's time i didn't add the two calls, but the result is same. – suitianshi Feb 11 '14 at 03:16
  • remove this listchoice.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); and the clean and build your project and run may be it works. – Jitesh Dalsaniya Feb 12 '14 at 04:33
  • Possible duplicate of [OnItemCLickListener not working in listview](https://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview) – Winter Sep 13 '17 at 14:35

4 Answers4

0

On method getView() of the adapter, you can set the onclick event of convertView before returning it. That event handler can be treated as onItemClick :)

VinhNT
  • 1,091
  • 8
  • 13
  • Yes, i know this. But while I'm also using `listSelector`. I have tested that if i set click listener for the textView, the list selector will not work. So what should i do then? – suitianshi Feb 11 '14 at 03:56
0

The listview click listener not working when we use buttons,imagebutton etc. As you use only textview so there will not be problem like that... As you used linearlayout no need to use android:focusable="false" in your linear layout. We used this code only during usage of buttons. Also no need to use code holder.mTextView.setClickable(false); As text is not a button so it takes no any focus. When u clicked it will click on the list cell not on the textview... So Simply inflate and then after setting custom adapter to the listview... setonitemclicklistner to the list view....

UPDATED ANSWER

convertView.setOnClickListener(new OnItemClickListener( position ));

This will surely works...

Kimmi Dhingra
  • 2,249
  • 22
  • 21
0

I just ran into the same problem, and tried all suggested solutions but none worked for me. After trying out different things, I found out there was another reason for my problem. I was wrapping my list_item inside a ScrollView, which was causing my onItemClick to not be called. I hope this helps someone.

0

One thing that seems to override the row clicks is if you have textviews, make sure they don't have an android:inputtype associated with them remove the inputtype and you can click the row This is in addition of course to the above answers on

james
  • 113
  • 1
  • 1
  • 10