setOnClickListener of a ListView not working

前端 未结 5 1555
挽巷
挽巷 2020-12-09 04:06

So I\'m trying to set up a setOnClickListener for my ListView but it\'s causing a crash in my program for some reason when I try... I\'m quite new

5条回答
  •  甜味超标
    2020-12-09 05:12

    firstly set the value inside your main activity where your using your listview:

    new android.widget.AdapterView.OnItemClickListener()
    

    in place of adapter of your listview:

      new AdapterView.OnItemClickListener() 
    

    So finally it looks like:

    this.listView.setItemsCanFocus(false);
    this.listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(android.widget.AdapterView parent, View view, int position, long id) {
    
     }
    

    when you are using any custom view to populate the data inside listview.

    Then in your main Activity file.xml in the parent layout{ inside which listview is there} set this option:

        android:descendantFocusability="blocksDescendants"
    

    and if you are using any buttons/textview inside listview, set this property on that textview/button:

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

    For scroll option in your listview just add this line inside activity:

     listView=(ListView)findViewById(R.id.container);
     listView.setNestedScrollingEnabled(true);
    

    As per i understood i am giving answer, and i hope it will help, if anyone found some corrections or helpful, they are welcome.

提交回复
热议问题