When we use the ListView, the essential use of adapter,adapter is like a plumbing interface that combines the data you want to show with the layout style you want to show through some sort of agreement.
Arrayadapter for each list item you want to show, you need to define a arrayadapter, and here's how to define a plumbing interface you want:
For each item, you need to set a corresponding data class, for example, you want to show a list of cities, each with a textview and a CheckBox, show a city and its selection, in theory your data class will need to provide these want to show the data, For data and views to correspond, we need to rewrite the GetView method: "getView (int position, view Convertview, ViewGroup parent)" The parameter means: position: The currently drawn item is subscript in the adapter, and you can also understand that you are drawing the view of the first data source; Convertview: The current incoming list view, possibly empty, may not be empty (reuse the currently invisible view);
1 if (Convertview = null 2 Convertview =
Getactivity (). Getlayoutinflater ()
null 4 log.i (TAG, "line" 5 }
The above is a very important code, it means that only when the inspection of the incoming drawing view is empty, the first view is added to the list, the meaning of reuse view is to minimize the need to consume views, imagine there are more than 100 items of the list, your screen can only display 10 of them, Then you need to maintain a 90-item view that is not needed for the time being, 1% utilization, so we need to reuse the view, and when the view goes down, when an item disappears, the view is passed into the GetView function that the new item is drawn as a parameter: Convertview, Re-replication: Suppose my data source is a cityitem, there are two values one is the city name, one is whether the city is selected, in order to be able to show, my layout options "r.layout.list_item_crime" need to have a textview and CheckBox to assign a value after locating the corresponding component via Findviewbyid:
1 @Override2 PublicView GetView (Final intposition, View Convertview, ViewGroup parent) {3 if(Convertview = =NULL) {4Convertview =getlayoutinflater (). Inflate (5 R.layout.city_list_item,NULL)///The layout style that you want to show, contains a TextView and Checkbox,id are R.id.city_name and r,id.isselected respectively .6 }7Cityitem C =Cityitems.get (position);8TextView Titleview =(TextView) Convertview9 . Findviewbyid (R.id.city_name);//Find components by IDTen Titleview.settext (C.getname ());//Assignment One ACheckBox Selectedcheckbox =(CheckBox) Convertview - . Findviewbyid (r.id.isselected); - the selectedcheckbox.setchecked (c.getselected ()); - returnConvertview; -}
You can then customize a construction method,
1 Public Cityadapter (arraylist<cityitem> crimes,context context) {2 Super(context, 0 , crimes); 3 }
This way I build an adapter with only the data source and context that I care about, so a custom arrayadapter is written;
How to use it? Find the corresponding ListView view, add the adapter,
1 New This ); 2 Citylist.setadapter (cityadapter);
The use of Android Listview--arrayadapter learning