After writing the basic layout, the next lesson we will learn how to use Android is a very important, but for beginners slightly difficult to the ListView, even a long time ago, someone has said, will not write the ListView is the first step of Android can get started.
Well, say so much, write this tutorial is also hope that we can deepen the impression, preview in advance, in order to avoid listening to Sharon confused.
As shown in the interface is a ListView, as the name implies, the ListView is a list control that is populated with a content shim called adapter.
This is the ListView schematic, in which the cursor everyone without the tube, the cursor involves the operation of the database, the main look at a line from ArrayList-----"Adapter-------" ListView.
From this step we can see that the process of using a ListView is to put the data (Arraylist), and then the receiver (Apapter) into the ListView.
One of the ArrayList can be thought of as a list of anything that can be stored in the form of a arraylist< object >.
1. First we need an item style, which is what each item in the list looks like.
This is a style that we need, where an icon appears, a text, a clickable box (checkbox).
1<?xml version= "1.0" encoding= "Utf-8"?>2<Relativelayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Android:layout_width= "Match_parent"5android:layout_height= "Wrap_content" >6<ImageView7Android:id= "@+id/list_img"8Android:layout_margin= "8DP"9android:src= "@mipmap/ic_launcher"TenAndroid:layout_width= "32DP" Oneandroid:layout_height= "32DP"/> A<TextView -Android:id= "@+id/list_text" -Android:singleline= "true" theandroid:text= "haha haha haha" -Android:layout_centervertical= "true" -android:layout_torightof= "@+id/list_img" -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content"/> -<CheckBox +Android:layout_centervertical= "true" AAndroid:layout_alignparentright= "true" atAndroid:layout_gravity= "Right" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content"/> -</RelativeLayout>
Of course, as our default layout style, it's best not to fill anything inside.
2. Now that we have a layout, we need to create a relative data object to deposit three content.
1 Public classListinfo {2 Private intlist_img;3 PrivateString List_text;4 Private BooleanList_click;5 6 PublicListinfo (intList_img, String List_text,BooleanList_click) {7 This. list_img =list_img;8 This. List_text =List_text;9 This. List_click =List_click;Ten } One A Public intgetlist_img () { - returnlist_img; - } the - PublicString Getlist_text () { - returnList_text; - } + - Public BooleanIslist_click () { + returnList_click; A } at}
This class contains the three information in the view, a resource ID, a text, and a Boolean value that is clicked.
3. The most important receiver came, creating a new class, inheriting Baseadapter:
After inheritance, four methods are overwritten by default.
1 @Override2 Public intGetCount () {3 return0;4 }5 6 @Override7 PublicObject GetItem (inti) {8 return NULL;9 }Ten One @Override A Public LongGetitemid (inti) { - return0; - } the - @Override - PublicView GetView (intI, view view, ViewGroup ViewGroup) { - return NULL; +}
From the name you can see what these methods are for, GetCount get the total number, GetItem returns an object for each item, Getitemid directly returns I, GetView gets the view of each item.
In addition to these, we need two more things, a contextual context for looking for a view, a ArrayList is what we use to fill the data, these two things to be done through the constructor function.
1 Private Arraylist<listinfo> userlist; 2 Private Context context;
ArrayList the object that we have just created is the listinfo.
constructor function:
Public ListAdapter (context context, arraylist<listinfo> userlist) { this. Context= context; this. userlist = userlist; }
With these things we can modify the previous three methods:
@Override publicint GetCount () { return Userlist.size (); } @Override public Object getItem (int i) { return Userlist.get (i); } @Override publiclong getitemid (int i) { return i; }
The Size,get item with the data, and I return, respectively.
4. The following is the most important thing about GetView:
We all know that layout and data connection is done by first finding the layout, then populating the data, but the ListView has a lot of children, each time to find a layout and refill is a very consumption of system functions,
So we use the Viewholder way to cache the layout, to prevent the consumption of multiple polling.
The so-called Viewholder is to cache the layout through an inner class:
Private class viewholder{ private ImageView ImageView; Private TextView TextView; Private checkbox checkbox; }
This class contains all the controls we need for a layout, a ImageView, a textview, a checkbox, and this class as an inner class for listadapter.
And then this is GetView:
1 @Override2 PublicView GetView (intI, view view, ViewGroup ViewGroup) {3 //start by creating a new Viewholder instance, because the GetView function will run once every item is generated,4 //That's why we're using this notation.5Viewholder Viewholder =NULL;6 //view or NULL, this is the first time you create7 if(View = =NULL){8 //new instance9Viewholder =NewViewholder ();Ten //add a layout to the view, where view is the item's view OneView = Layoutinflater.from (context). Inflate (R.layout.list_item,NULL); A //Fill for Viewholder -Viewholder.imageview =(ImageView) View.findviewbyid (r.id.list_img); -Viewholder.textview =(TextView) View.findviewbyid (r.id.list_text); theViewholder.checkbox =(CheckBox) View.findviewbyid (R.id.list_check); - //set additional tags to view to store one data, we put Viewholder in - View.settag (viewholder); -}Else { + //This is already >=2 created item. - //take the view out of the tag +Viewholder =(Viewholder) View.gettag (); A } at //find data for current item from ArrayList -Listinfo Listinfo =Userlist.get (i); - //Populate individually - ViewHolder.imageView.setImageResource (Listinfo.getlist_img ()); - ViewHolder.textView.setText (Listinfo.getlist_text ()); - viewHolder.checkBox.setChecked (Listinfo.islist_click ()); in returnview; -}
The notes are written in great detail.
5. Use in activity:
The layout adds:
1 <ListView2 android:id= "@+id/listview"3 android:layout_width= "Wrap_ Content "4 android:layout_height=" Wrap_content "5 />
Add in activity:
1ListView ListView =(ListView) Findviewbyid (R.id.listview);2 3Arraylist<listinfo> ArrayList =NewArraylist<>();4 5 for(inti = 0;i < 10;i++){6Arraylist.add (NewListinfo (r.mipmap.ic_launcher, "Clam clam",true));7Arraylist.add (NewListinfo (R.mipmap.ic_launcher, "clam",false));8 }9 TenListAdapter adapter =NewListAdapter ( This, arrayList); One AListview.setadapter (adapter);
Notice that the new ArrayList is created and then randomly new with 20 subkeys.
Then fill in the adapter, and then add adapter to the ListView.
This is the last finished interface! Everybody try it!
Source Download
Basic application of the ListView