Android getting started tutorial (18)-listview (2 ))

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/

 

Listview is a commonly used control. Each item in the listview can be a string or a combination control. First, let's talk about the implementation of listview:

1. PrepareData ;

2. with one-dimensional or multi-dimensional dynamic array save data.

2. build adapter , in simple terms, the adapter is item array , dynamic array each element generates multiple items.

3. Adapter Add to listview and display it.


Next, let's take a look at this articleCodeImplemented listview:

 

Next, we will start the XML code of the UI:

The main. XML Code is as follows:

View plain Copy to clipboard Print ?
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Linearlayout
  3. Android: ID="@ + ID/linearlayout01"
  4. Android: layout_width="Fill_parent"
  5. Android: layout_height="Fill_parent"
  6. Xmlns: Android=Http://schemas.android.com/apk/res/android">
  7. <Listview Android: layout_width="Wrap_content"
  8. Android: layout_height="Wrap_content"
  9. Android: ID="@ + ID/mylistview">
  10. </Listview>
  11. </Linearlayout>

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> Android: Id = "@ + ID/linearlayout01" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> xmlns: Android = "http://schemas.android.com/apk/res/android"> </P> <p> <listview Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/mylistview"> <br/> </listview> <br/> </linearlayout> <br/>

 

 

The code for my_listitem.xml is as follows. my_listitem.xml is used to design the items of listview:

View plain Copy to clipboard Print ?
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Linearlayout
  3. Android: layout_width="Fill_parent"
  4. Xmlns: Android=Http://schemas.android.com/apk/res/android"
  5. Android: Orientation="Vertical"
  6. Android: layout_height="Wrap_content"
  7. Android: ID="@ + ID/mylistitem"
  8. Android: paddingbottom="3dip"
  9. Android: paddingleft="10dip">
  10. <Textview
  11. Android: layout_height="Wrap_content"
  12. Android: layout_width="Fill_parent"
  13. Android: ID="@ + ID/itemtitle"
  14. Android: textsize="30dip">
  15. </Textview>
  16. <Textview
  17. Android: layout_height="Wrap_content"
  18. Android: layout_width="Fill_parent"
  19. Android: ID="@ + ID/itemtext">
  20. </Textview>
  21. </Linearlayout>

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> Android: layout_width = "fill_parent" <br/> xmns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: orientation = "vertical" <br/> Android: layout_height = "wrap_content" <br/> Android: Id = "@ + ID/mylistitem" <br/> Android: paddingbottom = "3dip" <br/> Android: paddingleft = "10dip"> <br/> <textview <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" <br/> Android: Id = "@ + ID/itemtitle" <br/> Android: textsize = "30dip"> <br/> </textview> <br/> <textview <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" <br/> Android: id = "@ + ID/itemtext"> <br/> </textview> <br/> </linearlayout> <br/>

 

Here are some of the attributes used:

1. paddingbottom = "3dip", layout leaves 3 pixels of blank area at the bottom

2. paddingleft = "10dip", layout leaves a blank area of 10 pixels to the left

3. textsize = "30dip", textview font is as large as 30 pixels.

 

Finally, JavaSource code:

View plain Copy to clipboard Print ?
  1. Public VoidOncreate (bundle savedinstancestate ){
  2. Super. Oncreate (savedinstancestate );
  3. Setcontentview (R. layout. Main );
  4. // Bind the listview in XML as the item container
  5. Listview list = (listview) findviewbyid (R. Id. mylistview );
  6. // Generate a dynamic array and reload the data
  7. Arraylist NewArraylist
  8. For(IntI =0; I <30; I ++)
  9. {
  10. Hashmap <string, string> map =NewHashmap <string, string> ();
  11. Map. Put ("Itemtitle","This is title .....");
  12. Map. Put ("Itemtext","This is text .....");
  13. Mylist. Add (MAP );
  14. }
  15. // Generate the adapter, array ===" listitem
  16. Simpleadapter mschedule =NewSimpleadapter (This,// No explanation
  17. Mylist,// Data Source
  18. R. layout. my_listitem,// XML Implementation of listitem
  19. // Subitem of the dynamic array and listitem
  20. NewString [] {"Itemtitle","Itemtext"},
  21. // Two textview IDs in the XML file of listitem
  22. New Int[] {R. Id. itemtitle, R. Id. itemtext });
  23. // Add and display
  24. List. setadapter (mschedule );
  25. }
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.