Android Common Data Adapter Simpleadapter

Source: Internet
Author: User

The Arrayadapter data adapter is described in "Android Common data Adapter Arrayadapter". However, there is a flaw, that is, the item icon is fixed the same, to show that each item's icon is not the same, then use Simpleadapter

After you create a new project, create a new List_item.xml file under the Layout folder, and then edit the layout with the following code:

<?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:gravity= "Center_vertical"android:orientation= "Horizontal" >    <ImageViewAndroid:id= "@+id/iv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>

The code in Activity_main.xml is as follows:

<Relativelayoutxmlns: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"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " >    <ListViewAndroid:id= "@+id/lv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" /></Relativelayout>

to copy a few pictures from \sdk\platforms\android-18\data\res\drawable-hdpi . in the drawable-hdpi folder

The code is as follows:

 PackageCom.wuyudong.simpleadapter;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ListView LV=(ListView) Findviewbyid (r.id.lv); List<map<string, object>> data =NewArraylist<map<string, object>>(); Map<string, object> map1 =NewHashmap<string, object>(); Map1.put ("Nametext", "I am the 1th feature"); Map1.put ("iconID", R.drawable.btn_radio_off_disabled_focused_holo_dark); Map<string, object> map2 =NewHashmap<string, object>(); Map2.put ("Nametext", "I am the 2nd feature"); Map2.put ("iconID", R.drawable.btn_radio_off_disabled_focused_holo_light); Map<string, object> map3 =NewHashmap<string, object>(); Map3.put ("Nametext", "I am the 3rd feature"); Map3.put ("iconID", R.drawable.btn_radio_off_focused_holo_dark); Map<string, object> MAP4 =NewHashmap<string, object>(); Map4.put ("Nametext", "I am the 4th feature"); Map4.put ("iconID", R.drawable.btn_radio_off_focused_holo_light); Map<string, object> map5 =NewHashmap<string, object>(); Map5.put ("Nametext", "I am the 5th feature"); Map5.put ("iconID", R.drawable.btn_radio_off_holo);        Data.add (MAP1);        Data.add (MAP2);        Data.add (MAP3);        Data.add (MAP4);        Data.add (MAP5); Lv.setadapter (NewSimpleadapter ( This, data, R.layout.list_item,NewString[] {"Nametext", "iconID"},New int[]{r.id.tv, R.ID.IV}]); }}

The effect after operation is as follows:

Android Common Data Adapter 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.