Android 12th Lesson--simpleadapter

Source: Internet
Author: User

Simpleadapter is not simple, his parameters took 5, God, dizzy.

Today this adapter, public simpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] F Rom, int[] to)

See this probably understand, the parameters are the first: to access the entire Android application interface, basically all the components need, generally write this (study it another day), the second should be the list object name, the third is the list of the ID, 4th, 5 refers to the contents of the map stored in his list, the string type and other types (his ID).

<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" ><ListViewAndroid:id= "@+id/mylist"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"    /></Relativelayout>
<?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"><!--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:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"><!--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:textsize= "20DP"Android:textcolor= "#f0f"Android:paddingleft= "10DP"    /><!--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:textsize= "14DP"Android:paddingleft= "10DP"    /></LinearLayout></LinearLayout>
 PackageCom.example.simpleadapter;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter; Public classMainactivityextendsActivity {//Create a ListView in XML that displays the list items for Simpleadapter    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[] imagesids=New int[]{r.drawable.tiger,r.drawable.nongyu,r.drawable.qingzhao,r.drawable.libai}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Create a list collection, set the element master 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", Imagesids[i]); Listitem.put ("PersonName", Names[i]); Listitem.put ("Desc", Descs[i]);        Listitems.add (ListItem); }        //Create a SimpleadapterSimpleadapter 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);    List.setadapter (Simpleadapter); }}

I think the implementation of this code is to create a ListView, implement Simpleadapter list items, and write the contents of the list items in the created Simpleadapter.

Remark: this study simpleadapter,list,hashmap.

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.