OnItemClick没有响应点击

时间:2013-03-15 10:19:03

标签: android listview onitemclick

已搜索listview OnClick not working pages,无法解决。

我的Activity通过调用自定义适配器填充listview MyArrayAddapter  它扩展了 ArrayAdapter 类。

问题:onItemClick()或setOnItemClick()没有响应。

当我点击任何项目时,不会执行任何操作,甚至不会执行日志语句。

<<>>

代码段:

在我的类中扩展Activity实现OnItemClickListener,我在OnCreate()方法中初始化并调用以下函数:

    private void listViewSet() {

        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(  this,  "Component "+ rootListWrapper.get( position ).toString() + " clicked.", Toast.LENGTH_LONG ).show();

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

注意:当我点击列表视图的一个项目时,我得到完全相同的消息,只是时间戳不同。我得到7-8个这样的新消息。单击之前的LogCat:

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
点击后

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

活动的布局:

    <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>

行项目布局:

<?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>

它第一次完美填充,但我希望当我点击时我应该能够通过更改参数并再次调用setAdapter()方法来重新填充它。

解决这个问题肯定有助于其他许多人,因为我看到许多人都有未解之谜。

有问题的类的java文件:

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更新:在3个按钮上使用focusable:false属性后。

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)

2 个答案:

答案 0 :(得分:0)

编辑:

用这个

替换你的行xml文件
<?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>

您的自定义适配器可能具有可聚焦的项目,如(EditText,Button)等,尝试将行项目的焦点设置为false

答案 1 :(得分:0)

 listView.performClick();

会触发列表视图上的onClick(),而不是列表中的单个项目。

因此,您还必须实施onClick()方法来拦截列表视图上的点击。