The ListView list is a control that we often use. If you want to customize the display in it, it is very troublesome. You need to create the XML and Class SimpleAdapter files, which is more troublesome. What if we just want to display two or three lines of text on it, but don't want to bother? We only need to create a new XML file.
The following example shows three textviews in a ListView.
First, we need to create an XML file, which is used for the layout of a single ListView item.
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 is the content, and the third is the status.
Next, we need to place a ListView control in 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 need 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 of ListView data, used to store data displayed on ListView. Data Operations on SimpleAdapter directly affect the display of ListView.
Then, we will add some data to be displayed to the mList.
[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", "State ");
MList. add (map );
In this way, a piece of data is added. If you want to add multiple data entries, repeat the addition.
Next, we put the data in 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: parent pointer, ArrayList data, layout file, labels of the data to be displayed, and controls to be displayed. The order of the following two parameters must match.
Finally, you can load the ListView into SimpleAdapter.
Of course, the direct operation of mList will also affect the data of ListView. After modifying the mList data, call the notifyDataSetChanged () method of SimpleAdapter.
From KnowHeart-Kress