How to bind a baseadapter to custom data in Android

Source: Internet
Author: User
Tags phoneview

CopyCode The Code is as follows: public class personadapter extends Baseadapter {
Private list persons; // data to be bound
Private int resource; // the ID of the bound entry interface. In this example, It is item. xml.
Private layoutinflater Inflater; // layout filler, which can generate a view object using an XML file and obtain instance objects through context

Public personadapter (context, list persons, int Resource ){
Inflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );
This. Resource = resource;
This. Persons = Persons;
}

@ Override
Public int getcount () {// get the total number of data to be bound
Return persons. Size ();
}

@ Override
Public object getitem (INT position) {// specify the index value to obtain the object corresponding to the index value
Return persons. Get (position );
}

@ Override
Public long getitemid (INT position) {// get the entry ID
Return position;
}

// Listview has the cache function. When the first page is displayed, the page object is created. When the second page is displayed, the created object on the first page is reused.
// Entry acquisition page: Position indicates the index value of the data to be bound to the current entry in the set.
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Textview nameview = NULL;
Textview phoneview = NULL;
Textview amountview = NULL;
If (convertview = NULL) {// when the first page is displayed, convertview is empty.
Convertview = Inflater. Inflate (resource, null); // generate an 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 = Persons. Get (position );
// Implement 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.