How to add a click event to the listview. A new activity is opened when each row is clicked.

Source: Internet
Author: User

=============== Problem description ====================


Package COM. example. one_listview; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android Oid. widget. baseadapter; import android. widget. imageview; import android. widget. listview; import android. widget. textview; public class mainactivity extends activity {private listview lv; private list <Map <string, Object> data; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Lv = (listview) findviewbyid (R. id. LV); Data = getdata (); myad Apter adapter = new myadapter (this); LV. setadapter (adapter);} private list <Map <string, Object> getdata () {list <Map <string, Object> List = new arraylist <Map <string, object >>> (); Map <string, Object> map = new hashmap <string, Object> (); map. put ("title", "campus memories"); map. put ("info", "Youth is the lab course of life, and it is worthwhile to make a mistake. From laughter and forget song "); map. put ("IMG", R. drawable. one_1); list. add (MAP); map = new hashmap <string, Object> (); map. put ("title", "author"); map. put ("info", "Life is much easier if you don't ask about the road and the process. By Tao lixia "); map. put ("IMG", R. drawable. one_2); list. add (MAP); map = new hashmap <string, Object> (); map. put ("title", "lonely like a planet"); map. put ("info", "all sorrow always leaves a trace of joy. All regrets will always leave a perfect corner. I am looking for a gap in hope in the ice-proof deep ocean. But when I woke up at midnight, I suddenly caught a glimpse of the beautiful moonlight. From gaps "); map. put ("IMG", R. drawable. one_3); list. add (MAP); map = new hashmap <string, Object> (); map. put ("title", "before I become a lone hero"); map. put ("info", "How do you know what I have said to you when I face you silently. When I stood in front of you, how do you know that my heart has already turned around for you. By Li Zilin "); map. put ("IMG", R. drawable. one_4); list. add (MAP); return list;} static class viewholder {public imageview IMG; Public textview title; Public textview Info;} public class myadapter extends baseadapter {private layoutinflater minflater = NULL; private myadapter (context) {This. minflater = layoutinflater. from (context);} public int getcount () {return data. size ();} public object g Etitem (INT position) {return position;} public long getitemid (INT position) {return position;} public view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; If (convertview = NULL) {holder = new viewholder (); convertview = minflater. inflate (R. layout. list_item, null); holder. IMG = (imageview) convertview. findviewbyid (R. id. IMG); holder. title = (textview) conver Tview. findviewbyid (R. id. TV); holder.info = (textview) convertview. findviewbyid (r.id.info); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. IMG. setbackgroundresource (integer) data. get (position ). get ("IMG"); holder. title. settext (string) data. get (position ). get ("title"); holder.info. settext (string) data. get (position ). get ("info"); Return convertview; LV. setonit Emclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// do not understand. How to add .} });}}}

============= Solution 1 ======================


LV. setonitemclicklistener (New onitemclicklistener ()
{
 
@ Override
Public void onitemclick (adapterview <?> Parent, view, int position, long ID)
{
// Do not understand. How to add.
You can directly open the activity here. If you want to pass the data of the current item, you can use data. Get (position) to obtain the data of the current item.
}

});

============= Solution 2 ======================


LV. setonitemclicklistener
Written in oncreate

============= Solution 3 ======================


Do not write onitemclick In the adapter .. As the public said above, just add the Listener of an item, override an oncreat In the listener, and add the intent in oncreat.

============= Solution 4 ======================


Add intent () to the listener to directly jump to the listener.

How to add a click event to the listview. A new activity is opened when each row is clicked.

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.