ListView這個控制項使用的非常普遍,關於它的基本介紹,我們來看一下API中的介紹:Class OverviewA view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. 我們的LIstView中顯示的資料都是通過ListAdapter這個介面來配置的,通常是用它的某一個子類來配置資料,下面來介紹一下各種適配器以及他們的關係:ListAdapterimplements Adapterandroid.widget.ListAdapterKnown Indirect SubclassesArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, WrapperListAdapterClass OverviewExtended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter. 在ListAdapter的眾多子類當中,用的最多的就是ArrayAdapter(儲存數組)、SimpleAdapter(字串)、CursorAdapter(資料庫中的資料)。在使用LIstView控制項來顯示資料時,有兩種實現方式:第一種實現方式: 在xml中定義<LIstView>布局對象,設定它的相關屬性,然後在Activity中對它進行配置和事件監聽;第二種實現方式: 讓你的 Activity 類繼承 ListActivity,可以通過getListView()來擷取,可以不寫ListView的xml檔案。本篇文章將以第二種方式為例來做一個簡單的執行個體。首先來認識一下ListActivity這個類:java.lang.Object↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity ↳ android.app.ListActivity正如名字含義,它是Activity的一個子類。Class OverviewAn activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.ListActivity has a default layout that consists of a single, full-screen list in the center of the screen我可以直接使用內建的預設, full-screen list,當然很多時候要做一些擴充,我們可以自訂screen layout,使用setContentView() in onCreate()來顯示定義的ListView, 不過這裡有一要求,To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)這樣的原因是什麼呢?原因是我們繼承ListActivity這個類,使用getListView()來取得它,系統在執行該方法的時候是按照 id= list 去尋找,所以我們必須定義Id為" @android:id/list"。知道了吧! 我們通過一個在String.xml檔案裡面配置多個國家的名稱,然後通過適配器ArrayAdapter來顯示這些資料到我們的LIstView控制項中,並且提供根據使用者輸入的匹配符,篩選出匹配的國家名稱。運行結果:大家可以看到國家的名稱已經顯示在LIstView控制項當中了,當我們輸入"be"是自動匹配的結果:其實這個自動匹配功能很簡單,只要在代碼中調用這句代碼:listView.setTextFilterEnabled(true); 工程目錄結構: 以下是原始碼:MainActivity.java:[html] package com.listview.activity; import android.app.ListActivity; 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; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ListActivity implements OnItemClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] countries = getResources().getStringArray(R.array.countries_array); // 為ListView設定適配器 setListAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, countries)); ListView listView = getListView(); //設定Item的選中事件監聽器 listView.setOnItemClickListener(this); //這個屬性為true表示listview獲得當前焦點的時候,相應使用者輸入的匹配符,篩選出匹配的listview Items listView.setTextFilterEnabled(true); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, ((TextView)view).getText(), 1).show(); } } main.xml在這裡沒起作用,這裡就不給出代碼了。list_item.xml:[html] <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" > </TextView> strings.xml:[html] <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">HelloListView</string> <string-array name="countries_array"> <item>Bahrain</item> <item>Bangladesh</item> <item>Barbados</item> <item>Belarus</item> <item>Belgium</item> <item>Belize</item> <item>Benin</item> <item>Bermuda</item> <item>Bhutan</item> <item>Bolivia</item> <item>Bosnia and Herzegovina</item> <item>Botswana</item> <item>"Bouvet Island</item> <item>Brazil</item> </string-array> </resources>