In the first two, we have a general understanding of what the adapter is, and some of its basic knowledge.
And the two commonly used Android native adapters, arrayadapter and Simplecursoradapter , are packaged, although they are very simple to use, but less scalable.
Now we continue to drill down into the Android adapter, this time we will learn simpleadapter together
Simpleadapter is the best extensibility, you can define a variety of layouts, you can put on imageview (picture), you can also put a button (button), CheckBox (check box) and so on, of course, there are things that it cannot do, which we will say later.
Tip: Some code inherits the listactivity,listactivity and the normal activity does not have the big difference, the difference is to display the ListView to do many optimizations, the aspect displays only. We still inherit from normal activity.
The following example program we are going to write is to implement a class table with a picture. Of course we'll use simpleadapter .
The program starts!
(1) also first add a button in the Activity_main.xml, one will jump when used.
(2) then create a new class Simpleadapterdemo inherit from activity as our third example of activity, @Override our onCreate method.
(3) Create a new XML file Simpleadapterdemo.xml as our layout file, which also contains a text field and a ListView:
The code is as follows:
Simpleadapterdemo.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:text=" This is an example of Simpleadapter "> </TextView> <listview android: Id= "@+id/simpleadapterdemolistview" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "> </ListView></LinearLayout>
Watch out! Here and the first two can be different:
(4) we need to define the XML that good one uses to display the contents of each column
Listitem1.xml contains horizontal pictures and text, where extensibility is reflected!
We can completely define the contents of each column, for example, I can make each column a picture and add text underneath the picture.
The code is as follows:
Listitem1.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "Horizontal" > <imageview android:id= "@+id/imgview1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margin= "5px"/> <textview android:id= "@+id/text1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "#000000" Android: Textsize= "22px" android:layout_margin= "5px"/> <textview android:id= "@+id/text2" android:layout_width= " Wrap_content "android:layout_height=" wrap_content "android:textcolor=" #000000 "android:textsize=" 15px "Android: Layout_margin= "5px"/></linearlayout>
(5) or return to Simpleadapterdemo, define a ListView to be called the LV, and use just the Id,setcontentview () for the XML layout just now, and then set an adapter for the LV:
Use Simpleadapter The data used are generally HashMap Composition of List , List each section corresponds to ListView of each row. Each key-value data for the HASHMAP is mapped to the corresponding ID component in the layout file . since the system does not have a corresponding layout file available, we can define a layout ourselves listitem1. xml .
The following is a fitting, New a Simpleadapter
Simpleadapter adapter = new Simpleadapter (this,data,resource,from, to);
The parameters are: This,HashMap title and info , the layout file (listitem1. xml), as far as from and to, have a very familiar wood, on our previous article has just been used : From is your query data, to is a single-line layout of the individual controls, one-to-one relationship, it is very convenient to use. Each component of the layout file is mapped to each element of the HashMap, and the adaptation is done.
Here is the specific code:
Package Com.example.adapterdemo;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.app.activity;import Android.os.bundle;import Android.widget.listview;import Android.widget.simpleadapter;public class Simpleadapterdemo extends Activity {private ListView lv; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.simpleadapterdemo); LV = (ListView) Findviewbyid (R.id.simpleadapterdemolistview); Simpleadapter adapter = new Simpleadapter (this, GetData (), r.layout.listitem1,new string[] {"Text1", "Text2", "img"}, NE W int[] {r.id.text1, r.id.text2, r.id.imgview1}); Lv.setadapter (adapter);} Public list<map<string, Object>> GetData () {list<map<string, object>> List = new arraylist< Map<string, object>> (); map<string, object> map = new hashmap<string, object> (), Map.put ("Text1", "Image1"), Map.put ("Text2", "Info1 "); Map.put (" img ", R.DRawable.ic_launcher); List.add (map); map = new hashmap<string, object> (); Map.put ("Text1", "Image2"); Map.put (" Text2 "," Info2 "); Map.put (" img ", R.drawable.ic_launcher); List.add (map); map = new hashmap<string, object> (); Map.put ("Text1", "Image3"), Map.put ("Text2", "Info3"), Map.put ("img", R.drawable.ic_launcher); List.add (map); map = new Hashmap<string, Object> (), Map.put ("Text1", "Image4"), Map.put ("Text2", "Info4"), Map.put ("img", R.drawable.ic_ launcher); List.add (map); map = new hashmap<string, object> (); Map.put ("Text1", "Image5"); Map.put ("Text2", "Info5 "); Map.put (" img ", R.drawable.ic_launcher); List.add (map); map = new hashmap<string, object> (); Map.put (" Text1 "," Image6 "); Map.put (" Text2 "," Info6 "); Map.put (" img ", R.drawable.ic_launcher); List.add (map); map = new hashmap<string , object> (), Map.put ("Text1", "Image7"), Map.put ("Text2", "Info7"), Map.put ("img", R.drawable.ic_launcher); List.add (map); map = new hashmap<string, object> (); Map.put ("Text1", "Image8"); Map.put ("Text2", "Info8"), Map.put ("img", R.drawable.ic_launcher); List.add (map); return list;}}
And finally it's time to look at the results:
:
Such three commonly used Android native adapter even if finished, with a step-by-step study, I believe that the use of the adapter has been more experience. Have we found that the adapter we are using is getting more powerful, and of course the implementation process is more cumbersome.
There are also some Android native adapter, we an example learn the most use of the three native adapter, the other is a side dish, then we will have to customize the adapter, the custom adapter can adapt to more situations, more powerful, of course, we also need more in-depth study to apply freely, Please don't miss it!
The source code I will be in the last post to be uploaded together, within the same time I write the blog side to write code, so I think clear ideas.
I am also a student, the level is limited, also please many advice ~