Set the onclick event for the element in the listview item

Source: Internet
Author: User

Poor expression ability, so now I want to explain the meaning of the title:
The itemonclick event is triggered when you click the list item of the listview. After you click it, you can go to page A. If there is a connection in the subitem, you want to go to page B, what should we do. Now I can understand the solution:
There are two methods: rewrite the adapter, and set the onclick event in the element of your sub-item (note that the onclick element is set in XML)
I only used the first method and demonstrated it as follows:
We inherit the base class of the adapter, and then override getview.

@ Suppresswarnings ("unchecked") public view getview (INT position, view convertview, viewgroup parent) {convertview = minflater. inflate (this. resource, null); map item = data. get (position); int COUNT =. length; For (INT I = 0; I <count; I ++) {view v = convertview. findviewbyid (to); BindView (v, item, from);} // The following lines are highlights of view Avatar = convertview. findviewbyid (R. id. row_avatar); // obtain the component if (null! = Avatar) {// judge to save the trouble of avatar. settag (item); // we can use settag to store data and send it to the event triggering avatar. setonclicklistener (avatarclicklistener); // trigger event code to look down} return convertview;} // This is the trigger event. Write private onclicklistener avatarclicklistener = new onclicklistener () as required () {@ suppresswarnings ("unchecked") Public void onclick (view v) {map item = (MAP) v. gettag (); // Do you still remember the above settag? We can obtain intent mintent = new intent (adapterwithhashmap. this. context, myself. class); mintent. putextra ("user_id", item. get ("uid "). tostring (); adapterwithhashmap. this. context. startactivity (mintent );}};

 

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.