Add button response event in Android Simpleadapter, GetView Override

Source: Internet
Author: User

The ListView inside the Andriod is a control that displays the list data, the adapter simpleadapter is bound, and the binding code is as follows:
 This . Findviewbyid (R.id.listview1); Simpleadapter Adapter=new  simpleadapter (context, data, resource, from, to); Lstview.setadapter ( adapter);

Click event response for an element in the ListView list:
New Onitemclicklistener () {    @Overridepublicvoid Onitemclick (adapterview<?>      intlong  arg3) {         //  TODO auto-generated method Stub     }};listview.setonitemclicklistener (mitemclicklistener);

For button controls such as ImageButton to implement single events, let's look at how to implement such a feature:

Inherit Simpleadapter to override GetView method

Why not inherit the Baseadapter adapter for processing because Baseadapter is a very basic base class, and for generic textviwe, the Imageview,button control's data binding is not implemented

Java code:

Private classMysimpleadapterextendsSimpleadapter {@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        //TODO auto-generated Method StubView v =Super. GetView (position, convertview, parent); ImageButton btn=(ImageButton) V.findviewbyid (R.id.icon);        Btn.settag (position); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubMdatalist.remove ((int) V.gettag ());           Notifydatasetchanged (); Toast.maketext (Getapplicationcontext (), "Μ¥»÷îòáë" +v.gettag (), 1). Show ();                         }      }); returnv; }      PublicMysimpleadapter (Context context, List<?extendsmap<string,?>> data,intresource, string[] from,int[] to) {          Super(context, data, resource, from, to); //TODO auto-generated Constructor stub      }    }

When you click a button, the button event needs to know which item of the ListView is clicked, and you can add a tag for each button object, where we use the index of the ListView item position as the tag

Add button response event in Android Simpleadapter, GetView Override

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.