The ListView is a frequently used control, and each subkey inside the ListView item can make a string, or it can be a combined control. Let's talk about the ListView implementation:
1. Prepare the data for the ListView to be displayed ;
2. Using one-dimensional or multidimensional dynamic array save data;
2. Build adapter , < Span style= "font-size:medium;" To put it simply, adapter is Span style= "color: #008080;" >item array How many items are generated ;
3. Add the Adapter to the ListView and display it.
Next, you start the XML code for the UI:
The Main.xml code is as follows :
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutAndroid:id= "@+id/linearlayout01"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"> <ListViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/mylistview"> </ListView></LinearLayout>
The code for My_listitem.xml is as follows, My_listitem.xml is used to design the ListView item:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutAndroid: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"> <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:id= "@+id/itemtitle"android:textsize= "30dip"> </TextView> <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:id= "@+id/itemtext"> </TextView></LinearLayout>
Explain some of the properties that are used inside:
1.paddingbottom= "3dip", layout set aside 3 pixels of blank space at the bottom
2.paddingleft= "10dip", layout to left a blank area of 10 pixels
3.textsize= "30dip", TextView's font size is 30 pixels.
The last is the Java source code:
Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //The listview in the binding XML, as the container for itemListView list =(ListView) Findviewbyid (R.id.mylistview); //generate dynamic arrays, and reprint dataarraylistNewArraylist(); for(inti=0;i<30;i++) {HashMap<string, string> map =NewHashmap<string, string>(); Map.put ("Itemtitle", "This is the Title ..."); Map.put ("Itemtext", "This is text ..."); Mylist.add (map); } //build Adapter, array = = = "ListItemSimpleadapter Mschedule =NewSimpleadapter ( This,//nothing to explain .MyList//Data SourcesR.layout.my_listitem,//XML implementation of ListItem //the children of the dynamic array corresponding to the ListItem NewString[] {"Itemtitle", "Itemtext"}, //two TextView IDs in the ListItem XML file New int[] {r.id.itemtitle,r.id.itemtext}); //Add and showList.setadapter (Mschedule); }
Common usage of Android ListView (ii)