Android 之 ListView 点击响应代码?

来源:互联网 发布:nt数据看宝宝男女最准 编辑:程序博客网 时间:2024/06/06 05:47

本论坛将全面搬家到:http://www.cnblogs.com/91program,请大家以后来这里看看。


ListView 点击响应代码(如下),以前都是正确的:

private class MusicListItemClickListener implements OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {}}
可参阅以前的一博文:http://blog.csdn.net/91program/article/details/39232721

在新的工程使用时,先提示 onItemClick 需要删除 @Override。我直接将 onItemClick 删除,再自动增加函数时,发现自动增加的函数变成如下所示的,参数的参数发生了变化,特别是第一个参数,由 AdapterView 变成 AdapterViewCompat。代码不再提示错误,运行之!
private class contactsListItemClickListener implements OnItemClickListener {// AdapterViewCompat.OnItemClickListener@Overridepublic void onItemClick(AdapterViewCompat<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubint position = arg2;int id = (int) arg3;Log.i(phoneBookInfo.TAG, "position: " + Integer.toString(position) + " id: " + Integer.toString(id));}}

运行时出现错误。提示如下:
 
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 的成员,如下:
private class contactsListItemClickListener implements AdapterView.OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// TODO Auto-generated method stubLog.i(phoneBookInfo.TAG, "position: " + Integer.toString(position) + " id: " + Long.toString(id));}}

这样自动增加的 onItemClick 函数终于和以前一样了。
运行之,正确!
0 0