Simple use of the Android ListView list control

Source: Internet
Author: User

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

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.