Inheriting the Baseadapter class
Cover the following 4 methods:
@Override Public intGetCount () {returnusers.size (); } @Override PublicObject GetItem (intposition) { returnUsers.get (position); } @Override Public LongGetitemid (intposition) { return(User) GetItem (position)). GetId (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(convertview==NULL) {Convertview=inflater.inflate (Resouce,NULL); } //get the appropriate controlTextView id=(TextView) Convertview.findviewbyid (r.id.user_id); TextView name=(TextView) Convertview.findviewbyid (r.id.user_name); TextView Phone=(TextView) Convertview.findviewbyid (R.id.user_phone); User User=(User) getItem (position); //binding DataId.settext (string.valueof (User.getid ())); Name.settext (User.getname ()); Phone.settext (User.getphone ()); returnConvertview; }
The above code implements a simple ListView adapter,
GetCount () Gets the total number of bars for the current data
GetItem () Gets the specific object that can get the data according to position
Getitemid () Gets the ID of the data that is specific to the position
GetView () View object for item after binding data is obtained based on position and Convertview
The GetView () method can be slightly optimized in the following ways:
PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(convertview==NULL) {Convertview=inflater.inflate (Resouce,NULL); //Cache Control ObjectViewcontrol views=NewViewcontrol (); Views.id=(TextView) Convertview.findviewbyid (r.id.user_id); Views.name=(TextView) Convertview.findviewbyid (r.id.user_name); Views.phone=(TextView) Convertview.findviewbyid (R.id.user_phone); Convertview.settag (views); } Viewcontrol views=(Viewcontrol) Convertview.gettag (); User User=(User) getItem (position); Views.id.setText (String.valueof (User.getid ())); Views.name.setText (User.getname ()); Views.phone.setText (User.getphone ()); returnConvertview; } Private Final classviewcontrol{ PublicTextView ID; PublicTextView name; PublicTextView Phone; }
Personal Understanding:
Adapter in fact, and the computer network adapter, a kind of can be rubbed and two have a certain connection but not directly simple to join together. This is where you assume the ability to bind data to controls.
Customizing the ListView Adapter