--by San[[email protected]]
When writing a list, if it is a normal list, you can do it with ListView + Arrayadapter, but if you want to have an icon in front of each list item, or an icon in the back of the text, this is done with Simpleadapter. Don't be fooled by its name, it's not simple, it's powerful.
Let's take an example of a menu list with icons, first we create a layout file Menu_frame.xml, which contains a ListView with the following code:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 android:padding= "5DP"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <ListView8 Android:id= "@+id/menu_list"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One /> A - </LinearLayout>
We then create a menu item for the layout file Menu_items.xml, which contains a ImageView and a TextView, the code is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:id= "@+id/menu_item"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "Horizontal"7 Android:paddingtop= "40DP">8 9 <!--icon for menu item -Ten <ImageView One Android:id= "@+id/menu_img" A Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:paddingleft= "10DP" the android:contentdescription= "@string/hello_world" - /> - - <!--the text of the menu item - + <TextView - Android:id= "@+id/menu_name" + Android:layout_width= "Wrap_content" A Android:layout_height= "Wrap_content" at Android:paddingleft= "10DP" - android:textsize= "20SP" - /> - - </LinearLayout>
The layout of menus and menu items have been written, and now combine them.
1String[] Menunames = {"First", "second", "third", "Fouth" };2 int[] menuimg = {3 r.drawable.ic_top_bar_send_disabled,4 R.drawable.ic_about_us,5 R.drawable.ic_app_recommendation,6 R.drawable.ic_article_more_item_read_it_later7 };8list<map<string, object>> listItems =NewArraylist<map<string, object>>();9 for(inti = 0; i < menunames.length; i++ ) {Tenmap<string, object> ListItem =NewHashmap<string, object>(); OneListitem.put ("img", Menuimg[i]); AListitem.put ("name", Menunames[i]); - Listitems.add (listItem); - } theSimpleadapter Sampleadapter =NewSimpleadapter ( This - , ListItems - , R.layout.menu_items -,Newstring[] {"img", "name"} +,New int[] {r.id.menu_img, r.id.menu_name} - ); + AMenulistview =(ListView) Findviewbyid (r.id.menu_list); atMenulistview.setadapter (Sampleadapter);
This is mainly simpleadapter, say a few parameters meaning
- The second parameter is a collection of list<map<string,?>>, for each map<string in the collection,?> can generate a list item
- Specifies the ID of a layout interface
- The parameter is a string[] type parameter that determines the value of map<string, which key in the?> object, to generate the list item
- The parameter is a int[] type parameter that determines which builds are populated (the IDs defined in the corresponding menu_items.xml)