This article describes the display of the ListView, there are many adapters for the ListView, such as Arrayadapter,baseadapter,simpleadapter and so on. This article first warm up, introduce the simplest simpleadapter adapter.
For the Android interface display.
First, add the following code to the main interface layout file Main.xml:
<relativelayout xmlns: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 " tools:context= ". Mainactivity "> <listview android:id=" @+id/lv " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" /></relativelayout>
There is only one component that displays data: ListView.
Then, define a sub-layout file for the ListView item. It represents the layout of each entry for the list of ListView Item_listview.xml. As follows:
<?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=" wrap_content " android:o rientation= "Horizontal" > <imageview android:id= "@+id/iv_photo" android:layout_width= "40DP" android:layout_height= "40DP" android:src= "@drawable/photo3" /> <textview android: Id= "@+id/tv_name" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " Android:textsize= "22SP" android:layout_gravity= "center_vertical" /></linearlayout>
OK, now add the code for the data display in the mainactivity:
Package Com.itydl.arraysimple;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.simpleadapter;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Get ListView object ListView LV = (ListView) Findviewbyid ( R.ID.LV);//Each element in the collection contains all the data required for the ListView entry, and each entry in the case requires a string and an integer, so a Map is used to encapsulate the two data list<map<string, object> > data = new arraylist<map<string,object>> ();//define three MAP information map<string, object> map1 = new hashmap< String, object> (); Map1.put ("Photo", R.drawable.photo1), Map1.put ("name", "Xiao Zhi's son");d Ata.add (MAP1); map<string, object> map2 = new hashmap<string, object> (); Map2.put ("Photo", R.drawable.photo2); Map2.put (" Name "," Xiao Zhi ");d ata.adD (MAP2); map<string, object> map3 = new hashmap<string, object> (); Map3.put ("Photo", R.drawable.photo3); Map3.put (" Name "," Zhao Shuai ");d Ata.add (MAP3) lv.setadapter (new Simpleadapter (this, data, R.layout.item_listview, new string[]{" photo "," Name "}, new Int[]{r.id.iv_photo, r.id.tv_name});//note here the from and to two positions to correspond//is to set the key and value, in the layout file in which child nodes are displayed. Don't make a mistake or make a mess. }}
One thing to note: The new Simpleadapter (this, data, R.layout.item_listview,
New string[]{"Photo", "name"}, new Int[]{r.id.iv_photo, r.id.tv_name})
Parameter meaning: Context, data source, item layout file id,from,to. The from is an array, the key is the same as the map key, to is also the number of groups, indicating the component ID of the ITE display, note to be consistent with from the order, otherwise it will be an error.
OK, now run the program, the results are as follows:
Android Simple Combat Tutorial--18th, "ListView Display, Simple Adapter Simpleadapter"