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
?
- <?XML Version="1.0" Encoding="UTF-8"?>
-
- <Linearlayout
-
- Android: ID="@ + ID/linearlayout01"
-
- Android: layout_width="Fill_parent"
- Android: layout_height="Fill_parent"
-
- Xmlns: Android=Http://schemas.android.com/apk/res/android">
-
-
- <Listview Android: layout_width="Wrap_content"
- Android: layout_height="Wrap_content"
-
- Android: ID="@ + ID/mylistview">
-
- </Listview>
-
- </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
?
-
- <?XML Version="1.0" Encoding="UTF-8"?>
-
- <Linearlayout
- Android: layout_width="Fill_parent"
-
- Xmlns: Android=Http://schemas.android.com/apk/res/android"
-
- Android: Orientation="Vertical"
-
- Android: layout_height="Wrap_content"
- Android: ID="@ + ID/mylistitem"
-
- Android: paddingbottom="3dip"
-
- Android: paddingleft="10dip">
-
- <Textview
- Android: layout_height="Wrap_content"
-
- Android: layout_width="Fill_parent"
-
- Android: ID="@ + ID/itemtitle"
- Android: textsize="30dip">
-
- </Textview>
-
- <Textview
-
- Android: layout_height="Wrap_content"
- Android: layout_width="Fill_parent"
-
- Android: ID="@ + ID/itemtext">
-
- </Textview>
-
- </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
?
-
- Public VoidOncreate (bundle savedinstancestate ){
-
- Super. Oncreate (savedinstancestate );
-
- Setcontentview (R. layout. Main );
-
- // Bind the listview in XML as the item container
- Listview list = (listview) findviewbyid (R. Id. mylistview );
-
-
- // Generate a dynamic array and reload the data
-
- Arraylist NewArraylist
-
- For(IntI =0; I <30; I ++)
-
- {
- Hashmap <string, string> map =NewHashmap <string, string> ();
-
- Map. Put ("Itemtitle","This is title .....");
-
- Map. Put ("Itemtext","This is text .....");
-
- Mylist. Add (MAP );
-
- }
- // Generate the adapter, array ===" listitem
-
- Simpleadapter mschedule =NewSimpleadapter (This,// No explanation
-
- Mylist,// Data Source
- R. layout. my_listitem,// XML Implementation of listitem
-
- // Subitem of the dynamic array and listitem
-
- NewString [] {"Itemtitle","Itemtext"},
-
- // Two textview IDs in the XML file of listitem
- New Int[] {R. Id. itemtitle, R. Id. itemtext });
-
- // Add and display
-
- List. setadapter (mschedule );
-
- }