Function: Used to display large amounts of data in the form of item.
Steps to use:
1.XML in use <ListView> tags
2.dada-> string[] Array (data source)
3.arrayadapter<string> adapter=new arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1, data); –> adapter (used to bind data and controls
4. Mylistview.setadapter (adapter); –> Setting up the adapter
Ps.
Simple_list_item_1 (single-line display) only one TextView
Simple_list_item_2 (double row display) has two TextView
Two. Fixed-value ListView interface--Using Arrayadapter
Steps to use:
1. Customize the entity class.
2. Customize the item layout.
3. Customize the adapter to inherit the Arrayadapter.
4. Rewrite two methods, construct method Myadapter (context context, int resource, list<t> objects) and GetView () method.
Public classFruitadapterextendsArrayadapter<fruit> { Private intresourceId; Private intnum = 1;//the GetView method was called several times Private intNUM1 = 1;//re-created several times Convertview Private intnum2 = 1;//re-convertview a few times . PublicFruitadapter (Context context,intResource, list<fruit>objects) { Super(Context, resource, objects); ResourceId=resource; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {LOG.D ("H_bl", "num=" +num); Num+=1; Fruit Fruit=GetItem (position); View view; Viewholder Viewholder; LOG.D ("H_bl", "convertview=" +Convertview); if(Convertview = =NULL) {View= Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); Viewholder=NewViewholder (); Viewholder.fruitimage=(ImageView) View.findviewbyid (r.id.fruit_image); Viewholder.fruitname=(TextView) View.findviewbyid (r.id.fruit_name); View.settag (Viewholder); //store Viewholder in viewLOG.D ("H_bl", "num1=" +NUM1); NUM1+=1; } Else{View=Convertview; Viewholder= (Viewholder) view.gettag ();//Get Viewholder againLOG.D ("H_bl", "num2=" +num2); Num2+=1; } viewHolder.fruitImage.setImageResource (Fruit.getimage ()); ViewHolder.fruitName.setText (Fruit.getname ()); returnview; } classViewholder {ImageView fruitimage; TextView Fruitname; }}
which
null);
The inflate () function is somewhat similar to Findviewbyid (), looking for an XML layout file under Layout.
// Load Layout Manager Layoutinflater inflater=layoutinflater.from (context); // Convert an XML layout into a View object View view=inflater.inflate (R.layout.fruit_item,null); // using the View object, locate the control View.findviewbyid (r.id.fruit_image) in the layout;
How the ListView works:
1. The ListView requires adapter "Give me a View" (GetView) for each item in the list.
2. A new view is returned and displayed.
The summary is as follows:
See:
Http://www.bkjia.com/Androidjc/1037874.html
Http://www.bkjia.com/Androidjc/1037874.html
Three. Use Viewholder to improve the operation efficiency of the ListView:
Role:
1. Reuse Convertview (conversion view)--The view of the item that can be understood to be removed.
2. Cache the control instance.
When loading for the first time:
Sliding:
Explanation of principle:
// store Viewholder in view
1. Instance of the control into view, so that each item's view is bound to an instance of the control, enabling the cache of instances of the control.
2. Reuse the Convertview and remove the instance of the Convertview bound control and re-copy it. To achieve the purpose of reuse.
Possible problems with using Viewholder:
http://blog.csdn.net/harvic880925/article/details/25335957
Four. ListView Click events:
Listview.setonitemclicklistener (new Onitemclicklistener () { @Override public void int Long ID) { Fruit Fruit=fruitlist.get (position); Toast.maketext (Getapplicationcontext (), Fruit.getname (), Toast.length_short). Show (); } );
Android's ListView