Android simpleadapter displays listview and gridview

Source: Internet
Author: User

Simpleadapter, as a dataset, mainly fills the listview and gridview containers with data and summarizes several filling methods. The principle is very simple. You can directly look at the code and

1. Single Row display of listview(Simple_list_item_1)

Code:

Public class mylistitem1 extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> listview = new listview (this); </P> <p> List <Map <string, string> MList = new arraylist <Map <string, string> (); <br/> for (INT I = 0; I <10; I ++) {<br/> Map <string, string> map = new hashmap <string, string> (); <br/> map. put ("title", "test title"); <br/> map. put ("content", "Test content"); <br/> MList. add (MAP); <br/>}</P> <p> simpleadapter adapter = new simpleadapter (this, <br/> MList, <br/> android. r. layout. simple_list_item_1, // list display a row of Item1 <br/> New String [] {"content"}, // "title", <br/> New int [] {android. r. id. text1} <br/>); </P> <p> listview. setadapter (adapter); <br/> setcontentview (listview); <br/>}< br/>}

Effect:

2. listview dual-row display(Simple_list_item_2)

Code:

Public class mylistitem2 extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> listview = new listview (this); </P> <p> List <Map <string, string> MList = new arraylist <Map <string, string> (); <br/> for (INT I = 0; I <10; I ++) {<br/> Map <string, string> map = new hashmap <string, string> (); <br/> map. put ("title", "test title"); <br/> map. put ("content", "Test content"); <br/> MList. add (MAP); <br/>}</P> <p> simpleadapter adapter = new simpleadapter (this, <br/> MList, <br/> android. r. layout. simple_list_item_2, // list display Item1, item2 <br/> New String [] {"title", "content"}, <br/> New int [] {android. r. id. text1, android. r. id. text2} <br/>); </P> <p> listview. setadapter (adapter); <br/> setcontentview (listview); <br/>}< br/>}

Effect:

3. Custom display of listview

Code:

Public class mylist extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. mylist); </P> <p> List <Map <string, Object> MList = new arraylist <Map <string, Object> (); <br/> for (INT I = 0; I <10; I ++) {<br/> Map <string, Object> map = new hashmap <string, object> (); <br/> map. put ("pic", R. drawable. PIC); // load image resources <br/> map. put ("title", "test title"); <br/> map. put ("content", "Test content"); <br/> MList. add (MAP); <br/>}</P> <p> simpleadapter adapter = new simpleadapter (this, <br/> MList, <br/> r. layout. listitem, // custom Layout format <br/> New String [] {"pic", "title", "content "}, <br/> New int [] {R. id. listitem_pic, R. id. listitem_title, R. id. listitem_content }< br/>); </P> <p> listview = (listview) findviewbyid (R. id. list); <br/> listview. setadapter (adapter); <br/>}< br/>}Custom listitem. xml

<? XML version = "1.0" encoding = "UTF-8"?> </P> <p> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "? Android: ATTR/listpreferreditemheight "> </P> <p> <imageview Android: Id =" @ + ID/listitem_pic "<br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_alignparenttop = "true" <br/> Android: layout_alignparentbottom = "true" <br/> Android: adjustviewbounds = "true" <br/> Android: padding = "10dip"/> </P> <p> <textview Android: id = "@ + ID/listitem_title" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_torightof = "@ ID/listitem_pic" <br/> Android: layout_alignparentright = "true" <br/> Android: layout_alignparenttop = "true" <br/> Android: layout_alignwithparentifmissing = "true" <br/> Android: gravity = "center_vertical" <br/> Android: textsize = "22sp"/> </P> <p> <textview Android: id = "@ + ID/listitem_content" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_torightof = "@ ID/listitem_pic" <br/> Android: layout_alignparentbottom = "true" <br/> Android: layout_alignparentright = "true" <br/> Android: layout_below = "@ ID/listitem_title" <br/> Android: singleline = "true" <br/> Android: ellipsize = "marquee" <br/> Android: textsize = "14sp"/> <br/> </relativelayout>

Effect:

4. Custom display of gridview

Code:

Public class mygrid extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. mygrid); </P> <p> List <Map <string, Object> MList = new arraylist <Map <string, Object> (); <br/> for (INT I = 0; I <10; I ++) {<br/> Map <string, Object> map = new hashmap <string, object> (); <br/> map. put ("pic", R. drawable. PIC); // load image resources <br/> map. put ("title", "test title"); <br/> MList. add (MAP); <br/>}< br/> simpleadapter adapter = new simpleadapter (this, <br/> MList, R. layout. griditem, // custom Layout format <br/> New String [] {"pic", "title" },< br/> New int [] {R. id. griditem_pic, R. id. griditem_title ,}< br/>); </P> <p> gridview = (gridview) findviewbyid (R. id. grid); <br/> gridview. setadapter (adapter); <br/>}< br/>}Custom gridview. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_width = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <imageview Android: id = "@ + ID/griditem_pic" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center_horizontal"> <br/> </imageview> </P> <p> <textview Android: Id = "@ + ID/griditem_title" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center_horizontal"> <br/> </textview> <br/> </linearlayout>

Effect:

Source code download

Related Article

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.