The use of Android development Simpleadapter

Source: Internet
Author: User

Using Arrayadapter can only display TextView, the content of the display is relatively single. You can use Simpleadapter to make each item appear richer. The following will implement a sample, first.

First, look at the XML code, which also requires a main layout and a layout that represents each list item. As follows:

Main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"    ><!--Define a list -<ListViewAndroid:id= "@+id/mylist"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"    /></LinearLayout>

Main.xml actually has only one ListView control.

Simple_item.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= "Wrap_content"android:orientation= "Horizontal" >    <!--defines a imageview that is used as part of a list item.  -    <ImageViewAndroid:id= "@+id/header"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingleft= "10DP" />    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        <!--defines a textview that is used as part of a list item.  -        <TextViewAndroid:id= "@+id/name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingleft= "10DP"Android:textcolor= "#f0f"android:textsize= "20DP" />        <!--defines a textview that is used as part of a list item.  -        <TextViewAndroid:id= "@+id/desc"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingleft= "10DP"android:textsize= "14DP" />    </LinearLayout></LinearLayout>

Simple_item.xml has a imageview and two TextView, which are given at the beginning of the article to know the display of each list item.

The following will give the implementation code,

 Public classSimpleadaptertestextendsActivity {Privatestring[] names =NewString[] {"Tiger Head", "Get Jade", "Li Qingzhao", "Li Bai" }; Privatestring[] Descs =NewString[] {"Lovely child", "a girl who is good at music", "a woman who specializes in literature",            "Romantic Poet" }; Private int[] Imageids =New int[] {r.drawable.tiger, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai}; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //Create a list collection with the elements of the list set as maplist<map<string, object>> listItems =NewArraylist<map<string, object>>();  for(inti = 0; i < names.length; i++) {Map<string, object> ListItem =NewHashmap<string, object>(); Listitem.put ("Header", Imageids[i]); Listitem.put ("PersonName", Names[i]); Listitem.put ("Desc", Descs[i]);        Listitems.add (ListItem); }       //Create a Simpleadapter       /** Simpleadapter (context context, list<? extends Map<string,?>> data, * int resource, Strin G[] From, int[] to) * Context * Data: element is a list collection of Map * resource: Layout file for list item * from: Used to display data to list item Key Value Collection * To: The collection of control IDs in the resource used to display the data for the list item, and note that it corresponds to the display Data key value set one by one above*/Simpleadapter Simpleadapter=NewSimpleadapter ( This, ListItems, R.layout.simple_item,NewString[] {"PersonName", "header",                        "Desc" },                New int[] {r.id.name, R.id.header, R.id.desc}); ListView List=(ListView) Findviewbyid (r.id.mylist); //set adapter for the ListViewList.setadapter (Simpleadapter); }}

Compared to Arrayadapter, you can find that the implementation of the steps is still the three steps, set the data, set the adapter, binding adapter, but some of the steps are more complex, as follows:

1. Set the data: The data set here must be a list of map elements, map can put more than one key value pair;

2. Set up the adapter: In addition to passing in the list item layout file ID and the corresponding data collection, you also need to set the key value of the data to display to the list item, as well as the collection of IDs for the controls in the corresponding layout file

The use of Android development Simpleadapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.