Android's ListView Click Response code?

Source: Internet
Author: User

The ListView Click Response Code (below) was previously correct:

1 Private class Implements Onitemclicklistener {  2   3   4    @Override   5      Public void int position,long  id) {  6    }  7 }  

Refer to the previous blog post: http://blog.csdn.net/91program/article/details/39232721

When the new project is used, first prompt Onitemclick to delete the @Override. I directly delete the Onitemclick, and then automatically add the function, found that the automatically added function becomes as follows, the parameters of the parameter has changed, especially the first parameter, from Adapterview to Adapterviewcompat. Code no longer prompts for errors, run it!

1 Private classContactslistitemclicklistenerImplementsOnitemclicklistener {//Adapterviewcompat.onitemclicklistener2 @Override3      Public voidOnitemclick (adapterviewcompat<?> arg0, View arg1,intArg2,4             LongArg3) {  5         //TODO auto-generated Method Stub6         intPosition =arg2; 7         intID = (int) Arg3; 8LOG.I (Phonebookinfo.tag, "position:" + integer.tostring (position) + "ID:" +integer.tostring (id)); 9     }  Ten}

There was an error at run time. Tips are as follows:

D/androidruntime (19395): Shutting down VM W/DALVIKVM (19395): Threadid=1:thread exiting with uncaught exception (group=0  X4001B6A8) 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.Han Dler.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 (193 ): 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): ...   One more

Comparing the previous code with the current code, the discovery may be caused by adapterviewcompat. Therefore, it is mandatory to declare onitemclicklistener as a member of Adapterview, as follows:

1 Private classContactslistitemclicklistenerImplementsAdapterview.onitemclicklistener {2 @Override3      Public voidOnitemclick (adapterview<?> Parent, view view,intposition,4             LongID) {5         //TODO auto-generated Method Stub6LOG.I (Phonebookinfo.tag, "position:" + integer.tostring (position) + "ID:" +long.tostring (id)); 7     }  8}

This automatically increases the Onitemclick function at last as before.
Run it, right!

Android's ListView Click Response code?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.