1, Simpleadapter is ArrayList and the ListView Bridge, this ArrayList worship each item is a map<string,?> type. Each map object in the ArrayList corresponds to data binding one by one for each item inside the listview.
2, the Simpleadapter constructor:
Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to)
Parameters:
1,context: Context.
2,data: Map-based list. Each item in data corresponds to each item inside the listview. Each item in data is a map type that contains the data needed for each row in the ListView.
3,resource: is a layout that can be referenced by a system or customized.
4,from: This is an array of names, each of which is used to index the Object of each item in the ArrayList array map<string,object>.
5,to: Inside is a textview array. These textview are represented in the form of an ID. For example: Android.r.id.text1, this text1 can be indexed in layout.
An example:
Activity_main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Name"android:textsize= "20SP"Android:textstyle= "Bold" /> <TextViewAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Age"android:textsize= "20SP"Android:textstyle= "Bold" /> </LinearLayout> <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > </ListView></LinearLayout>
Mylist.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <TextViewAndroid:id= "@+id/name"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:gravity= "Center" /> <TextViewAndroid:id= "@+id/age"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:gravity= "Center" /> </LinearLayout>
Mainactivity.java
Packagecom.example.listviewooo;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Window;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter; Public classMainactivityextendsActivity {PrivateListView ListView; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); //Constructor Simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, Int[] to)Simpleadapter adapter =NewSimpleadapter ( This,//context in a constructor.GetData (),//data to display in the ListViewR.layout.mylist,//The layout ID of the ListView, which can be customized with the system's own Newstring[]{"Name", "Age"},//From and to one by one, each display of a view key in the MyList New int[]{r.id.name, r.id.age}];//to and from, the value should be the ID of each view in the custom ListViewListView =(ListView) Findviewbyid (R.id.listview); //It's all Setadapter .Listview.setadapter (adapter); } PublicList<map<string, object>>GetData () {List<map<string, object>> list =NewArraylist<map<string, object>>(); for(inti = 1; I <= 20; i++) {Map<string, object> map =NewHashmap<string, object>(); Map.put ("Name", "Zhang San" +i); Map.put ("Age", 20 +i); List.add (map); } returnlist; }}
Simpleadapter of the ListView