Android 之 ListView 點擊響應代碼?
ListView 點擊響應代碼(如下),以前都是正確的:
private?class?MusicListItemClickListener?implements?OnItemClickListener?{ @Override public?void?onItemClick(AdapterView?parent,?View?view,?int?position,long?id)?{ } }
在新的工程使用時,先提示 onItemClick 需要刪除 @Override。我直接將 onItemClick 刪除,再自動增加函數(shù)時,發(fā)現(xiàn)自動增加的函數(shù)變成如下所示的,參數(shù)的參數(shù)發(fā)生了變化,特別是第一個參數(shù),由 AdapterView 變成 AdapterViewCompat。代碼不再提示錯誤,運行之!
private?class?contactsListItemClickListener?implements?OnItemClickListener?{ //?AdapterViewCompat.OnItemClickListener @Override public?void?onItemClick(AdapterViewCompat?arg0,?View?arg1,?int?arg2, long?arg3)?{ //?TODO?Auto-generated?method?stub int?position?=?arg2; int?id?=?(int)?arg3; Log.i(phoneBookInfo.TAG,?"position:?"?+?Integer.toString(position)?+?"?id:?"?+?Integer.toString(id)); } }
運行時出現(xiàn)錯誤。提示如下:
?
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
對比之前的代碼,與現(xiàn)在的代碼,發(fā)現(xiàn)可能是 AdapterViewCompat 引起的。所以強制將 OnItemClickListener 聲明為 AdapterView 的成員,如下:
private?class?contactsListItemClickListener?implements?AdapterView.OnItemClickListener?{ @Override public?void?onItemClick(AdapterView?parent,?View?view,?int?position, long?id)?{ //?TODO?Auto-generated?method?stub Log.i(phoneBookInfo.TAG,?"position:?"?+?Integer.toString(position)?+?"?id:?"?+?Long.toString(id)); } }
這樣自動增加的 onItemClick 函數(shù)終于和以前一樣了。
運行之,正確!