The ListView list is the control we often use, if you want to customize the inside of the display is very troublesome, need to create a new XML, Class simpleadapter These two files, more cumbersome. What if we just want to show two or three lines of text on top, but don't want to be so troublesome? Then we just need to create a new XML enough.
Here is an example of displaying three TextView in a ListView item.
First we will create an XML file that is used as a single ListView item layout.
List_row.xml
[Java]
<?xml version= "1.0" encoding= "UTF-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:background= "#ffffff"
>
<textview
Android:id= "@+id/textto"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "16dip"
Android:textcolor= "#333333"
/>
<textview
Android:id= "@+id/textown"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/textto"
Android:textsize= "12dip"
Android:textcolor= "#999999"
/>
<textview
Android:id= "@+id/textstate"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentright= "true"
Android:textsize= "14dip"
Android:textcolor= "#999999"
/>
</RelativeLayout>
The first TextView is the title, the second one is the content, the third is the state
Next we need to place a ListView control inside the main XML layout file
[HTML]
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical"
Android:background= "#ffffff"
>
<listview
Android:id= "@+id/list"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:background= "#ffffff"
></ListView>
</LinearLayout>
Then we're going to declare three member variables in the main activity.
[Java]
Private list<map<string, object>> mlist;
Private ListView Mlistview;
Private Simpleadapter Mlistadapter;
Mlist is used to store the data to be displayed
Simpleadapter is a container for the ListView data that holds the data displayed on the ListView. Data manipulation on Simpleadapter directly affects the display of the ListView.
Then, let's add some data to mlist to display.
[Java]
Mlist = new arraylist<map<string,object>> ();
[Java]
map<string, object> map = new hashmap<string, object> ();
Map.put ("First", "This is the title");
Map.put ("Next", "This is the content");
Map.put ("state", "status");
Mlist.add (map);
This adds a piece of data, and if you want to add more than one, repeat and add it.
And then we put the data into the Simpleadapter/listview.
[Java]
Mlistadapter = null;
Mlistadapter = new Simpleadapter (this, mlist, R.layout.list_row,
New string[]{"First", "Next", "state"},
New Int[]{r.id.textown, R.id.textto, r.id.textstate});
Mlistview.setadapter (Mlistadapter);
New Simpleadapter Parameters: The parent pointer, the ArrayList data, the layout file, the label of the data to display, and the controls to display. The following two parameter order must correspond.
Finally, the ListView is loaded into Simpleadapter.
Of course, our direct manipulation of mlist also affects the data in the ListView. After modifying the mlist data, call Simpleadapter's Notifydatasetchanged () method.