OnItemClick not responding to clicks

2019-08-17 03:03发布

问题:

have searched listview OnClick not working pages, doesn't solve.

my Activity populates listview by calling a custom adapter MyArrayAddapter which extends ArrayAdapter class.

The problem: onItemClick() or setOnItemClick() ain't responding.

when i click on any of the items no action takes place, not even log statements get executed.

<<>>

code snippet:

Under my class which extends Activity implements OnItemClickListener, I initialize and call following functions in OnCreate() method:

    private void listViewSet() {

        MyArrayAdapter adapter = new MyArrayAdapter(    this,   R.id.tvrow, rootListWrapper );
        // Assign adapter to ListView
        listView.setAdapter(    adapter     );  
        listView.setOnItemClickListener(    this    );  //******
        listView.performClick();
    }

Even toast doesnt show up :(

@Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Log.v(  "OnCLick", "asdfhjdf" );
        Toast.makeText(  this,  "Component "+ rootListWrapper.get( position ).toString() + " clicked.", Toast.LENGTH_LONG ).show();

        root = rootListWrapper.get(  position  );
        rootListWrapper = root.children;        
        this.listViewSet();
    }

Note: when i click on an item of the listview, i get exactly same messages only differing in timestamp. I get 7-8 such new messages. LogCat before click:

03-15 09:54:06.507: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.877: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.877: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.947: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:06.947: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.117: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.117: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 09:54:07.297: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0

after click:

03-15 10:05:36.458: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:36.458: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:36.480: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.817: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.817: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.917: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0
03-15 10:05:40.960: W/Trace(1825): Unexpected value from nativeGetEnabledTags: 0

The layout for the activity:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BigClass" >

   <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

layout for row items:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:padding="20dp"
      >     

    <TextView android:id="@+id/tvrow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Component" />

    <LinearLayout android:weightSum="9"   android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@id/tvrow"   >

    <Button      android:id="@+id/bL"
        android:layout_weight="4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/blue_right"        
         />    
    <Button
        android:id="@+id/bC
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:background="@drawable/component2"
         />
    <Button
        android:layout_weight="4"
        android:id="@+id/bR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:background="@drawable/blue_right"
        /> 
   </LinearLayout>
</RelativeLayout>

It populates perfectly for first time, but I want when I click i should be able to repopulate it by changing the parameters and calling the setAdapter() method again.

Solving this will surely help out many others, as i see many have unsolved doubts.

java file for class in question:

public class BigClass extends Activity implements OnItemClickListener{




    List<Node> rootListWrapper;
    Node root;
    ListView listView;
    GetMethodEx getJSONString;
    JSONObject jObject;
    BigClass HealthNext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_display);

        listView = (ListView) findViewById(R.id.mylist);
        rootListWrapper = new ArrayList<Node>();
        root = new Node();
        getJSONString = new GetMethodEx();
        jObject = new JSONObject();

        //values = new String[] { "Android", "iPhone", "WindowsMobile",
        //        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        //        "Linux", "OS/2" };

        this.jsonDataFetch();
        this.listViewSet();

    }
        private void jsonDataFetch() {
        // TODO Auto-generated method stub
         try{
             jObject=new JSONObject(    getJSONString.getInternetData() );           
             root = root.createNode(    jObject );  
             Log.v( "component",    jObject.getString("status") );
             rootListWrapper.add(   root    );
        }catch(Exception e){
            e.printStackTrace();
        }
        Log.v("component", rootListWrapper.toString());
    }


        private void listViewSet() {
            // Define a new Adapter
            // First parameter - Context
            // Second parameter - Layout for the row
            // Third parameter - ID of the TextView to which the data is written
            // Fourth - the Array of data

            MyArrayAdapter adapter = new MyArrayAdapter(    this,   R.id.tvrow, rootListWrapper );
            // Assign adapter to ListView
            listView.setAdapter(    adapter     );  
            listView.setOnItemClickListener(    this    );
            listView.performClick();
        }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Log.v(  "OnCLick", "asdfhjdf" );
        Toast.makeText(  BigClass.this,  "Component "+ rootListWrapper.get( position ).toString() + " clicked.",    Toast.LENGTH_LONG ).show();

        root = rootListWrapper.get(  position  );
        rootListWrapper = root.children;
        HealthNext.jsonDataFetch();
        HealthNext.listViewSet();
    }

}

LOGCAT updated: after using focusable:false attribute on 3 buttons.

03-15 10:57:44.798: V/OnCLick(958): asdfhjdf
03-15 10:57:44.952: D/AndroidRuntime(958): Shutting down VM
03-15 10:57:44.952: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-15 10:57:44.992: E/AndroidRuntime(958): FATAL EXCEPTION: main
03-15 10:57:44.992: E/AndroidRuntime(958): java.lang.NullPointerException
03-15 10:57:44.992: E/AndroidRuntime(958):  at com.example.@@@@@@.BigClass.onItemClick(BigClass.java:86)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.os.Handler.handleCallback(Handler.java:725)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.os.Looper.loop(Looper.java:137)
03-15 10:57:44.992: E/AndroidRuntime(958):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-15 10:57:44.992: E/AndroidRuntime(958):  at java.lang.reflect.Method.invokeNative(Native Method)
03-15 10:57:44.992: E/AndroidRuntime(958):  at java.lang.reflect.Method.invoke(Method.java:511)
03-15 10:57:44.992: E/AndroidRuntime(958):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-15 10:57:44.992: E/AndroidRuntime(958):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-15 10:57:44.992: E/AndroidRuntime(958):  at dalvik.system.NativeStart.main(Native Method)

回答1:

Edit:

Replace your row xml file with this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="true"
    android:padding="20dp"
      >     

    <TextView android:id="@+id/tvrow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Component" />

    <LinearLayout android:weightSum="9"   android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@id/tvrow"   >

    <Button  android:id="@+id/bL"
        android:layout_weight="4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
           android:focusable="false"
        android:background="@drawable/blue_right"        
         />    
    <Button
        android:id="@+id/bC"
        android:layout_weight="1"
   android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:background="@drawable/component2"
         />
    <Button
        android:layout_weight="4"
        android:id="@+id/bR"
      android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:background="@drawable/blue_right"
        /> 
   </LinearLayout>
</RelativeLayout>

Your custom adapter may have a focusable items like (EditText, Button) etc, try to set focus of row items to false



回答2:

 listView.performClick();

Would trigger onClick() on the listview and not on individual items in the list.

So, you should also have to implement the onClick() method to intercept clicks on your listview.