This article only introduces the common usage of the ListView control. For details about ListView, please go to the details of the attributes and methods of ListView.
1. Procedure for using ListView
ListView usually has the following three elements:
(1) Layout of each entry in ListView;
(2) Fill in the content into the ListView;
(3) The Adapter that integrates the content with the page.
Therefore, you can follow these steps to use ListView:
(1) create a layout file for the ListView entry (or use the layout provided by the Android SDK );
(2) create content filled in the ListView, such as strings and images;
(3) create an Adapter and bind it to the ListView.
Ii. Create a ListView using the default layout File
In this example, the default layout file of the Android SDK is android. R. layout. simple_list_item_1, so you only need to create the layout file of the main Activity.
ListViewTest. xml
Next is the Activity file.
ListViewTestActivity. java
Package com. blogtest; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class ListViewTestActivity extends Activity {private static String [] strs = new String [] {"1", "2", "3", "4 ", "5"}; // define the data to be displayed private ListView myListView;/** Called when the activity is first created. * // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. listviewtest); findWidgets (); setAdapter ();} private void findWidgets () {myListView = (ListView) findViewById (R. id. mylistview);} private void setAdapter () {myListView. setAdapter (new ArrayAdapter
(This, // in this example, ArrayAdapterandroid. R. layout. simple_list_item_1, strs); // use the layout file that comes with the system }}
The running result is:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yP2hotans9a24M/rotate + rotate/rotate + ICAgICA8L3A + rotate "brush: java;"> private void setAdapter () {myListView. setAdapter (new ArrayAdapter (This, android. R. layout. simple_list_item_multiple_choice, strs); myListView. setChoiceMode (ListView. CHOICE_MODE_MULTIPLE );}
The running result is:
4. Respond to touch events
To enable the Item in ListView to respond to the click event, you need to create an OnItemClickListener class and bind it to the ListView. The following is an Activity file:
package com.blogtest;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class ListViewTestActivity extends Activity {private static String[] strs = new String[] { "0", "1","2", "3", "4", "5" };private ListView myListView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findWidgets();setAdapter();setListener();}private void findWidgets() {myListView = (ListView) findViewById(R.id.mylistview);}private void setAdapter() {myListView.setAdapter(new ArrayAdapter
(this,android.R.layout.simple_list_item_1, strs));}private void setListener() {myListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView
arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubnew AlertDialog.Builder(ListViewTestActivity.this).setMessage("Clicked Line No." + arg2).setPositiveButton("Confirm", null).show();}});}}
The running result is:
In addition to OnItemClickListener, there are also OnItemLongClickListener, OnItemSelectedListener and other listeners.