onListItemClick is not working for listview?

前端 未结 9 2140
一整个雨季
一整个雨季 2020-11-30 11:29

Hi onListItemClick for listview is not working. Here i am fetching datas from SQLite using AsyncTask and displaying it in a list view. And i wants to do some actions when a

相关标签:
9条回答
  • 2020-11-30 12:08

    Add below code to your TextView in the XML

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

    and try again.

    Another simple solution: add android:descendantFocusability="blocksDescendants" to the root viewgroup.

    0 讨论(0)
  • 2020-11-30 12:13

    The workaround I found avoid the

    AdapterView.OnItemClickListener mMessageClickedHandler=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
    
            }
        };
    

    on the ListView, but exploit the Adapter constructor that takes a Context as parameter:

    myCustomAdapter=new MyCustomAdapter(ActivityName.this,...)
    

    Passing ActivityName.this is possible to cast the Context in the adapter's class as ActivityName in a safe way and use its methods working like callbacks:

    ((ActivityName)context).activityMethod()
    

    Given that the getView() method of the Adapter class has a position parameter, is possible to pass this value to the activityMethod(int position) in order to know which list item has been pressed into the Activity where the ListView is.

    0 讨论(0)
  • 2020-11-30 12:23

    I had the same symptoms, and it drove me crazy for a while. Adding android:focusable="false" for the list items as suggested above fixed the problem for me.

    But the real issue was that I had set android:textIsSelectable="true" for my list items (in response to a Warning generated by Eclipse); setting android:textIsSelectable="false" fixed the problem for me, and I did not need the android:focusable="false" option at all.

    0 讨论(0)
  • 2020-11-30 12:25

    I struggled with this for a while - none of the provided solutions worked for me. In the end I found that having a call to getListView() in my onViewCreated() method did the trick, although I have no idea why. This is for a Fragment rather than an Activity; not sure if this makes any difference.

    public class NewsListFragment extends ListFragment {
        private ListView listView;
        ...
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            newsListView = getListView();
        }
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // this now works as expected
        }
    }
    
    0 讨论(0)
  • 2020-11-30 12:27

    You should add android:focusable="false" for ListView row items to make ListView Clikable. Because the views in the row of ListView gain the focus so ListView is not focusable. So, in your case you can add android:focusable="false" to the TextViews of your ListView row.

    0 讨论(0)
  • 2020-11-30 12:27

    Only the line below worked for me:

    android:descendantFocusability="blocksDescendants"
    

    The whole list_view_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    
        <!--android:focusableInTouchMode="false"-->
        <!--android:focusable="false"-->
        <!--android:clickable="false"-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:layout_height="wrap_content">
            <CheckBox
                android:id="@+id/checkBox_list_view_checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/checkBox_list_view_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
    

    The above is to be used in getView like:

    convertView=LayoutInflater.from(getContext())
        .inflate(R.layout.list_view_item, parent, false);
    

    `

    0 讨论(0)
提交回复
热议问题