Methods for customizing data-binding Adapter Baseadapter in Android _android

Source: Internet
Author: User

Copy Code code as follows:

public class Personadapter extends Baseadapter{
Private List persons;//The data to bind to
The ID of an entry interface for the private int resource;//binding, in this case the Item.xml
Private Layoutinflater inflater;//layout filler, which can use an XML file to generate a view object that can be used to get instance objects through the context

Public Personadapter (context context, List persons, int resource) {
Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
This.resource = resource;
this.persons = persons;
}

@Override
public int GetCount () {//Get total number of data to bind
return Persons.size ();
}

@Override
public object GetItem (int position) {//Given index value, object corresponding to index value
return Persons.get (position);
}

@Override
public long getitemid (int position) {//Get Entry ID
return position;
}

The

 //ListView has caching, and when the first page is displayed, a Page object is created, and the first page is reused when the second page is displayed.
 //Get Entry interface: Position the index value of the data to bind for the current entry in the collection
  @Override
 public View getview (int position, View Convertview, ViewGroup parent) {
   TextView Nameview = null;
  textview phoneview = null;
  textview amountview = null;
  if (Convertview = = null) {//Convertview is empty when the first page is displayed
   convertview = Inflater.inflate (resource, null);//Generate Entry object
   nameview = (TextView) Convertview.findviewbyid ( R.id.name);
   phoneview = (TextView) Convertview.findviewbyid (R.id.phone);
   amountview = (TextView) Convertview.findviewbyid (r.id.amount);

Viewcache cache = new Viewcache ();
Cache.amountview = Amountview;
Cache.nameview = Nameview;
Cache.phoneview = Phoneview;
Convertview.settag (cache);
} else {
Viewcache cache = (Viewcache) convertview.gettag ();
Amountview = Cache.amountview;
Nameview = Cache.nameview;
Phoneview = Cache.phoneview;
}

Person person = persons.get (position);
Implementing Data Binding
Nameview.settext (Person.getname ());
Phoneview.settext (Person.getphone ());
Amountview.settext (Person.getamount ());
return convertview;
}

Private Final class Viewcache {
Public TextView Nameview;
Public TextView Phoneview;
Public TextView Amountview;
}
}

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.