Adapter: After the data has been processed in the adapter, it is displayed on the view
In general, for Arrayadapter, you simply pass an array and a style to Arrayadapter, and you can display the string array on the view with a list.
For example, the code below:
But, for example, the contact directory for our phone is not just a list, but a picture, and possibly a mobile phone number. Therefore, simple arrayadapter is not able to achieve this complex view.
We can use Simpleadapter to implement this complex view, but we need to design the style.
We create a new Android project, and then we define a ListView in the layout directory to hold the list:
Main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" wrap_content ><!--Define a list--><listview android:id= "@+id/mylist" android:layout_width= "Fill_parent" android:layout _height= "Wrap_content"/></linearlayout>
After defining this, we can write the Java code:
Simpleadaptertest.java:
Package Org.crazyit.ui;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import Java.util.map;import Android.app.activity;import Android.graphics.color;import Android.os.Bundle;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.textview;import Android.widget.toast;public Class Simpleadaptertest extends activity{private string[] names = new string[]{"Tiger Head", "Get Jade", "Li Qingzhao", "Li Bai"};p rivate string[] Descs = new string[]{"Cute Child", "a girl who is good at music", "a woman who specializes in literature", "Romantic poet"};//This is a collection of three images of the ID private int[] imageids = new int[]{r.drawable . Tiger, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai}, @Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Create a list collection, The elements of the list collection are maplist<map<string, Object>> listItems = new arraylist<map<string, object>> (); for (int i = 0; i < names.length; i++) {Map<str ing, object> listItem = new hashmap<string, object> (); Listitem.put ("header", Imageids[i]); Listitem.put (" PersonName ", Names[i]); Listitem.put (" desc ", Descs[i]); Listitems.add (ListItem);} Create a Simpleadaptersimpleadapter Simpleadapter = new Simpleadapter (this, Listitems,r.layout.simple_item, new String[] {"PersonName", "header", "desc"},new int[] {r.id.name, R.id.header, R.id.desc}); ListView list = (ListView) Findviewbyid (r.id.mylist);//Set Adapterlist.setadapter for ListView (Simpleadapter);// Click Event-bound event listener for a ListView list item List.setonitemclicklistener (new Onitemclicklistener () {///position to fire the method when clicked. @Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long id) {Toast.maketext ( Getapplicationcontext (), names[position]+ "clicked", 1). Show ();}); The method is fired when List.setonitemselectedlistener (new Onitemselectedlistener () {//position is selected. @Overridepublicvoid Onitemselected (adapterview<?> parent, View view,int position, long id) {Toast.maketext ( Getapplicationcontext (), names[position]+ "was selected", 1). Show (); @Overridepublic void onnothingselected (adapterview<?> parent) {}});}}
in the Java code above, when creating the Simpleadapter object, we used a view: R.layout.simple_item, this is our custom style, you can use this style to implement a complex view.
Simple_item.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" wrap_content The ><!--defines a imageview that is used as part of the list item. --><imageview android:id= "@+id/header" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:paddingleft=" 10DP "/><linearlayoutandroid:orientation=" vertical "android:layout_width=" Match_parent "android:layout_height=" wrap_content "><!--defines a textview that is used as part of the list item. --><textview android:id= "@+id/name" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:textsize=" 20DP "android:textcolor=" #f0f "android:paddingleft=" 10DP "/><!--define a textview that is used as part of the list item. --><textview android:id= "@+id/desc" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:textsize=" 14DP "android:paddingleft=" 10DP "/></linearlayout></linearlayout>
combining these together can create the following effect:
Android Development Series (19): Set style for Simpleadapter