Android study notes 28 -------------- android ListView details

Source: Internet
Author: User

In android development, ListView is a commonly used component that displays specific content in the form of a list and can be automatically displayed based on the length of data.


The display of the list requires three elements:

1. ListVeiw is used to display the View of the list.

2. the adapter is used to map data to the mediation on the ListView.

3. The specific string, image, or basic component of the data to be mapped.

Based on the list of adapter types, the list is divided into three types: ArrayAdapter, SimpleAdapter, and SimpleCursorAdapter

ArrayAdapter is the simplest and can only display one line of words. SimpleAdapter has the best scalability and Can Customize various effects. SimpleCursorAdapter can be considered as a simple

A single combination can display the database content in the form of a list.

We start with the simplest ListView:


[Java]
Public class ListViewActivity extends Activity
{
Private ListView listView;
 
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
ListView = new ListView (this );
ListView. setAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_expandable_list_item_1, getData ()));
SetContentView (listView );
 
}
 
Private List <String> getData ()
{
List <String> data = new ArrayList <String> ();
Data. add ("test data 1 ");
Data. add ("Test data 2 ");
Data. add ("Test data 3 ");
Data. add ("test data 4 ");
Return data;
}
}
Public class ListViewActivity extends Activity
{
Private ListView listView;

@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
ListView = new ListView (this );
ListView. setAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_expandable_list_item_1, getData ()));
SetContentView (listView );

}

Private List <String> getData ()
{
List <String> data = new ArrayList <String> ();
Data. add ("test data 1 ");
Data. add ("Test data 2 ");
Data. add ("Test data 3 ");
Data. add ("test data 4 ");
Return data;
}
}

 


The layout file can be empty.

 

[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
 

</LinearLayout>

 


The above Code uses ArrayAdapter (Context context, int textViewResourceId, List <T> objects) to assemble data. to assemble the data, you need to connect the ListView view object and the number of groups.

Data adapter to adapt the two. The ArrayAdapter structure requires three parameters, namely this, layout file (note that the layout file here describes the layout of each row of the list,

Android. R. layout. simple_list_item_1 is a layout file defined by the system that only displays one line of text, a data source (a List set ). At the same time, use setAdapter () to complete the final work of adaptation. Realistic structure after running

For example:

 

 

SimpleCursorAdapter

The sdk is interpreted as follows: An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which

Columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views. Simply put, it is convenient

The data obtained from the cursor is displayed in the list, and the specified column can be mapped to the corresponding TextView.

The following program displays contacts in the phone book to the class table. Add a contact to the address book as the database data. Then obtain a Cursor pointing to the database and define a layout file (of course, you can also

Use the built-in system ).


[Java]
Private ListView listView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

ListView = new ListView (this );

Cursor cursor = getContentResolver (). query (ContactsContract. Contacts. CONTENT_URI, null );
StartManagingCursor (cursor );

ListAdapter listAdapter = new SimpleCursorAdapter (this, android. R. layout. simple_expandable_list_item_1,
Cursor,
New String [] {ContactsContract. Contacts. DISPLAY_NAME },
New int [] {android. R. id. text1 });

ListView. setAdapter (listAdapter );
SetContentView (listView );
}
 


SimpleAdapter

SimpleAdapter has the best scalability. It can define various la S, put ImageView, Button, and CheckBox. The following code is inherited directly.

There is no big difference between ListActivity, ListActivity and common Activity. The difference is that the display ListView has been optimized and displayed in some aspects.

The following program implements a class table with images.

First, you must define an xml file to display the content of each column.


[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
 
<RelativeLayout
Android: orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

<ImageView
Android: id = "@ + id/img"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margin = "2px"/>

<TextView
Android: id = "@ + id/title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ id/img"
Android: textColor = "# FFFFFFFF"
Android: textSize = "20px"/>

<TextView
Android: id = "@ + id/info"
Android: layout_toRightOf = "@ id/img"
Android: layout_below = "@ id/title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# FFFFFFFF"
Android: textSize = "13px"/>

</RelativeLayout>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<RelativeLayout
Android: orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

<ImageView
Android: id = "@ + id/img"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margin = "2px"/>

<TextView
Android: id = "@ + id/title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ id/img"
Android: textColor = "# FFFFFFFF"
Android: textSize = "20px"/>

<TextView
Android: id = "@ + id/info"
Android: layout_toRightOf = "@ id/img"
Android: layout_below = "@ id/title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# FFFFFFFF"
Android: textSize = "13px"/>

</RelativeLayout>

</LinearLayout>


The following is the implementation code:

[Java]
Public class ListViewActivity extends ListActivity
{
 
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SimpleAdapter adapter = new SimpleAdapter (this, getData (),
R. layout. main, new String []
{"Title", "info", "img"}, new int []
{R. id. title, R.id.info, R. id. img });
SetListAdapter (adapter );
 
}
 
Private List <Map <String, Object> getData ()
{
List <Map <String, Object> list = new ArrayList <Map <String, Object> ();
 
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("title", "name1 ");
Map. put ("info", "info 1 ");
Map. put ("img", R. drawable. icon );
List. add (map );
 
Map = new HashMap <String, Object> ();
Map. put ("title", "name2 ");
Map. put ("info", "info 2 ");
Map. put ("img", R. drawable. icon );
List. add (map );
 
Map = new HashMap <String, Object> ();
Map. put ("title", "name3 ");
Map. put ("info", "info 3 ");
Map. put ("img", R. drawable. icon );
List. add (map );
 
Return list;
}
 
}
Public class ListViewActivity extends ListActivity
{

@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SimpleAdapter adapter = new SimpleAdapter (this, getData (),
R. layout. main, new String []
{"Title", "info", "img"}, new int []
{R. id. title, R.id.info, R. id. img });
SetListAdapter (adapter );

}

Private List <Map <String, Object> getData ()
{
List <Map <String, Object> list = new ArrayList <Map <String, Object> ();

Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("title", "name1 ");
Map. put ("info", "info 1 ");
Map. put ("img", R. drawable. icon );
List. add (map );

Map = new HashMap <String, Object> ();
Map. put ("title", "name2 ");
Map. put ("info", "info 2 ");
Map. put ("img", R. drawable. icon );
List. add (map );

Map = new HashMap <String, Object> ();
Map. put ("title", "name3 ");
Map. put ("info", "info 3 ");
Map. put ("img", R. drawable. icon );
List. add (map );

Return list;
}

}

Data Using simpleAdapter is generally a List composed of HashMap. Each section of the list corresponds to each row of the ListView. Each key-value data of HashMap is mapped to the component corresponding to the id in the layout file. Because the system does not

The corresponding layout file is available. We can define a layout vlist. xml by ourselves. The following is an adaptation. The new SimpleAdapter parameter is: this, layout file (main. xml), HashMap title and

Info, img. The component id, title, info, and img of the layout file. The components of the layout file are mapped to the elements of HashMap to complete adaptation.

The running effect is as follows:

 


From running snails


Related Article

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.