The constructor of simpleadapter is as follows:
Simpleadapter (context, list <? Extends Map <string,?> Data, int resource, string [] From, int [])
A simlpleadapter works. Assume that simpleadapter is used for listview. Each list item in listview is the layout specified by the resource parameter value. The data parameter is the data to be loaded into the listview. Let's first look at each list item. Assume that the layout file corresponding to the list item contains two components: textview and edittext, with the ID textview and edittext respectively. When loading a list item, you must use the component ID to correspond to the map object in the list element in the Data parameter. Therefore, the from parameter is the key of the map object, and to indicates the component ID. For example, the parameter value in this example is from
= New string [] {"textview", "edittext"}, To = new int [] {R. Id. textview, R. Id. edittext }. This means that the value of the key in the map object as textview is bound to the R. Id. textview, and edittext is similar.
Now let's look at the data parameter. A listview consists of multiple list items. Each list item provides data by a map object, while multiple list items provide multiple map objects by the list object. OK, it's that simple, as long as you understand their working principles, everything can be done. If you still don't understand it, make an example. Good luck!
This is a small example. If you don't understand anything, you can do it.
This is the list_item.xml file.
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: Id = "@ + ID/ve_id_1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Textview
Android: Id = "@ + ID/ve_id2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>
This is the activitylist2 file.
Public class activitylist2 extends activity {
Listview;
Arraylist <Map <string, Object> data;
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Listview = new listview (this );
// Create a simpleadapter and use the system layout to display the two items
// Simpleadapter adapter = new simpleadapter (this, Data, Android. R. layout. simple_list_item_2,
// New String [] {"name", "gender"}, new int [] {R. Id. text1, R. Id. text2 });
// Create a simpleadapter and use the layout created by yourself to display the two items
Simpleadapter adapter = new simpleadapter (this, Data, R. layout. simple_list_item_2,
New String [] {"name", "gender"}, new int [] {COM. jiao. list. r. id. ve_id_1, Com. jiao. list. r. id. ve_id2 });
Listview. setadapter (adapter );
Setcontentview (listview );
}
Public void preparedata (){
Data = new arraylist <Map <string, Object> ();
Map <string, Object> item;
Item = new hashmap <string, Object> ();
Item. Put ("name", "Michael Jacob ");
Item. Put ("gender", "male ");
Data. Add (item );
Item = new hashmap <string, Object> ();
Item. Put ("name", "John ");
Item. Put ("gender", "female ");
Data. Add (item );
Item = new hashmap <string, Object> ();
Item. Put ("name", "Zhang Yupeng ");
Item. Put ("gender", "female ");
Data. Add (item );
}
}