Android study notes 29: simpleadapter

Source: Internet
Author: User

Adapter is an important part of Android. Many controls need to be used as data sources. This program uses simpleadapter as the listview data source.


The layout file contains a listview and the imageview and textview displayed in the listview.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Define a list --> <listview Android: Id = "@ + ID/mylist" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <! -- Define an imageview as part of the list item. --> <Imageview Android: Id = "@ + ID/Header" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "10dp"/> <! -- 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 = "16dp" Android: gravity = "center_vertical" Android: paddingleft = "10dp"/> </linearlayout>


Use List, imageview, and textview to construct simpleadapter.

Public class simpleadaptertest extends activity {private string [] names = new string [] {"kite", "Perfect Day", "time", "Stefanie "}; private int [] imageids = new int [] {R. drawable. sunyz_1, R. drawable. sunyz_6, R. drawable. sunyz_7, R. drawable. sunyz_8}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // create a list set. The elements of the list set are maplist <Map <string, Object> listitems = new arraylist <Map <string, Object> (); for (INT I = 0; I <names. length; I ++) {Map <string, Object> listitem = new hashmap <string, Object> (); listitem. put ("CDS", imageids [I]); listitem. put ("cdname", Names [I]); listitems. add (listitem) ;}// create a simpleadaptersimpleadapter simpleadapter = new simpleadapter (this, listitems, R. layout. main, new string [] {"cdname", "CDS"}, new int [] {R. id. name, R. id. header}); listview list = (listview) findviewbyid (R. id. mylist); // set adapterlist for listview. setadapter (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.