Android 之 ListView 点击响应代码?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 之 ListView 点击响应代码?相关的知识,希望对你有一定的参考价值。
ListView 点击响应代码(如下),以前都是正确的:
1 private class MusicListItemClickListener implements OnItemClickListener { 2 3 4 @Override 5 public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 6 } 7 }
可参阅以前的一博文:http://blog.csdn.net/91program/article/details/39232721
在新的工程使用时,先提示 onItemClick 需要删除 @Override。我直接将 onItemClick 删除,再自动增加函数时,发现自动增加的函数变成如下所示的,参数的参数发生了变化,特别是第一个参数,由 AdapterView 变成 AdapterViewCompat。代码不再提示错误,运行之!
1 private class contactsListItemClickListener implements OnItemClickListener { // AdapterViewCompat.OnItemClickListener 2 @Override 3 public void onItemClick(AdapterViewCompat<?> arg0, View arg1, int arg2, 4 long arg3) { 5 // TODO Auto-generated method stub 6 int position = arg2; 7 int id = (int) arg3; 8 Log.i(phoneBookInfo.TAG, "position: " + Integer.toString(position) + " id: " + Integer.toString(id)); 9 } 10 }
运行时出现错误。提示如下:
D/androidRuntime(19395): Shutting down VM
W/dalvikvm(19395): threadid=1: thread exiting with uncaught exception (group=0x4001b6a8)
E/AndroidRuntime(19395): FATAL EXCEPTION: main
E/AndroidRuntime(19395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hs.leozheng.backuprecords/com.hs.leozheng.backuprecords.MainActivity}: java.lang.ClassCastException: com.hs.leozheng.backuprecords.MainActivity$contactsListItemClickListener
E/AndroidRuntime(19395): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1734)
E/AndroidRuntime(19395): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1753)
E/AndroidRuntime(19395): at android.app.ActivityThread.access$1500(ActivityThread.java:155)
E/AndroidRuntime(19395): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999)
E/AndroidRuntime(19395): at android.os.Handler.dispatchMessage(Handler.java:130)
E/AndroidRuntime(19395): at android.os.Looper.loop(SourceFile:351)
E/AndroidRuntime(19395): at android.app.ActivityThread.main(ActivityThread.java:3820)
E/AndroidRuntime(19395): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19395): at java.lang.reflect.Method.invoke(Method.java:538)
E/AndroidRuntime(19395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
E/AndroidRuntime(19395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
E/AndroidRuntime(19395): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(19395): Caused by: java.lang.ClassCastException: com.hs.leozheng.backuprecords.MainActivity$contactsListItemClickListener
E/AndroidRuntime(19395): at com.hs.leozheng.backuprecords.MainActivity.findViews(MainActivity.java:140)
E/AndroidRuntime(19395): at com.hs.leozheng.backuprecords.MainActivity.onCreate(MainActivity.java:57)
E/AndroidRuntime(19395): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
E/AndroidRuntime(19395): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
E/AndroidRuntime(19395): ... 11 more
对比之前的代码,与现在的代码,发现可能是 AdapterViewCompat 引起的。所以强制将 OnItemClickListener 声明为 AdapterView 的成员,如下:
1 private class contactsListItemClickListener implements AdapterView.OnItemClickListener { 2 @Override 3 public void onItemClick(AdapterView<?> parent, View view, int position, 4 long id) { 5 // TODO Auto-generated method stub 6 Log.i(phoneBookInfo.TAG, "position: " + Integer.toString(position) + " id: " + Long.toString(id)); 7 } 8 }
这样自动增加的 onItemClick 函数终于和以前一样了。
运行之,正确!
以上是关于Android 之 ListView 点击响应代码?的主要内容,如果未能解决你的问题,请参考以下文章
listview中的每一个Item都有edittext,如何能够让listview的item和edittext都能响应点击消息
《Android开发艺术探索》之Android性能优化ListView和RecyclerView(十七)
Android ListView OnItemClickListener 对比。查看 OnClickListener