ListActivity:
一:相關(guān)概念簡(jiǎn)介(翻譯自官方文檔的class overview)
An 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是一個(gè)用于通過(guò)綁定到諸如array和Cursor等data source并陳列一系列item,當(dāng)用戶(hù)選中一個(gè)item的時(shí)候,會(huì)傳出event handler。
ListActivity擁有一個(gè)可以綁定到data source(尤其指帶有query result的array或Cursor)的ListView對(duì)象。 Binding,screen layout和row在下面討論。
(1)screen layout
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)
Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.
ListActivity有唯一一個(gè)充滿(mǎn)整個(gè)screen并位于screen正中的list的默認(rèn)layout。當(dāng)然,如果你想的話,你可以用onCreate()里面的setContentView()來(lái)透過(guò)設(shè)置自己的layout來(lái)定制這個(gè)screen layout。如果你要這樣做,你定制的layout里面務(wù)必要包含一個(gè)ListView的object,而且它的id必須為“@android:id/list”(括號(hào)里面沒(méi)看懂什么意思)
另外,你定制的view可以包含另外一個(gè)任何種類(lèi)的view對(duì)象,當(dāng)list view為空的時(shí)候它會(huì)顯現(xiàn)出來(lái),類(lèi)似地,它的id必須為“@id/android:empty”。
(2)row layout:
You can specify the layout of individual rows in the list. You do this by specifying a layout resource in the ListAdapter object hosted by the activity (the ListAdapter binds the ListView to the data; more on this later).
A ListAdapter constructor takes a parameter that specifies a layout resource for each row. It also has two additional parameters that let you specify which data field to associate with which object in the row layout resource. These two parameters are typically parallel arrays.
Android provides some standard row layout resources. These are in the R.layout class, and have names such as simple_list_item_1, simple_list_item_2, and two_line_list_item. The following layout XML is the source for the resource two_line_list_item, which displays two data fields,one above the other, for each list row.
你可以制定list每一個(gè)單獨(dú)的row的layout。當(dāng)你這樣做時(shí),你可以通過(guò)設(shè)置ListAdapter對(duì)象來(lái)決定使用哪個(gè)layout resource。一個(gè)ListAdapter的constructor至少設(shè)置一個(gè)parameter來(lái)設(shè)定layout resource。同時(shí)它有著兩個(gè)additonal parameter讓你制定哪些data field應(yīng)該對(duì)應(yīng)到哪個(gè)layout resource中的對(duì)象中去。這兩個(gè)parameter通常是parallel array(我理解成一一對(duì)應(yīng)的兩個(gè)數(shù)組,比如說(shuō)key-value pair就是一種parallel array,具體參看下面樣例代碼中的SimpleAdapter的定義過(guò)程,共五個(gè)參數(shù),其中第三個(gè)時(shí)上面說(shuō)的哪個(gè)設(shè)定layout resource的參數(shù),最后兩個(gè)就是那對(duì)parallel array了)
android提供了一些標(biāo)準(zhǔn)的row layout resource。他們?cè)?font face="DejaVu Serif, serif">R.layout類(lèi)里面,比如說(shuō):
Simple_list_item_1 每項(xiàng)有一個(gè)TextView
Simple_list_item_2 每項(xiàng)有兩個(gè)TextView
Simple_list_item_checked 帶CheckView的項(xiàng)
Simple_list_item_multiple_choise 每項(xiàng)有一個(gè)TextView并可以多選
Simple_list_item_single_choice 每項(xiàng)有一個(gè)TextView,但只能進(jìn)行單選。
(3)Binding to data:
You
bind the ListActivity's ListView object to data using a class that
implements the ListAdapter
interface. Android provides two standard list adapters: SimpleAdapter
for static data (Maps), and SimpleCursorAdapter
for Cursor query results.
你需要通過(guò)一個(gè)implement了ListAdapter接口的類(lèi)來(lái)把ListActivity的ListView object綁定到data上。Android提供了兩個(gè)標(biāo)準(zhǔn)的list adapters:針對(duì)static data(maps)使用的Simple Adapter和針對(duì)Cursor query result使用的SimpleCursorAdapter。
二,小零碎:
android:drawSelectorOnTop 默認(rèn)為false,而且為true的時(shí)候顏色會(huì)把選項(xiàng)菜單中的字給蓋住。(原文是:When set to true, the selector will be drawn over the selected item. Otherwise the selector is drawn behind the selected item.)
三,個(gè)人的理解和樣例代碼:
簡(jiǎn)單來(lái)說(shuō),ListActivity就是一個(gè)捆綁了List的Activity,但是他比普通的Activity上外加一個(gè)List的建立過(guò)程要簡(jiǎn)單得多。
普通的Activity要添加List,首先要在layout文件中定義一個(gè)ListView,但ListActivity里面則可以連setContentView()都不用寫(xiě),Android系統(tǒng)會(huì)自動(dòng)給你分配一個(gè)占據(jù)全屏幕的List對(duì)象。另外,在設(shè)置ListAdapter的時(shí)候,系統(tǒng)提供了一些默認(rèn)的布局(比如simple_list_item_2等),可以省去自定義List布局的功夫(當(dāng)然有必要的時(shí)候也是可以自定義List的布局的)。
總體來(lái)說(shuō),ListActivity就是為了簡(jiǎn)化List建立過(guò)程而提供的一個(gè)Activity的子類(lèi)。
下面是我自己測(cè)試的代碼。(沒(méi)有layout文件因?yàn)橛貌簧稀?/p>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListActivityTest extends ListActivity {
private static final String HashMap = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 使用了ListActivity自帶的的布局,可以注釋掉setContentView()
// setContentView(R.layout.main);
// 定義存放列表的數(shù)據(jù)結(jié)構(gòu)
List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
HashMap<String, String> user1 = new HashMap<String, String>();
HashMap<String, String> user2 = new HashMap<String, String>();
HashMap<String, String> user3 = new HashMap<String, String>();
// 為每個(gè)數(shù)據(jù)項(xiàng)填充數(shù)據(jù)
user1.put("user_name", "level0");
user1.put("user_phone", "135******");
user2.put("user_name", "lisi");
user2.put("user_phone", "1356*****");
user3.put("user_name", "wangwu");
user3.put("user_phone", "13544****");
// 把數(shù)據(jù)項(xiàng)添加到列表當(dāng)中
items.add(user1);
items.add(user2);
items.add(user3);
// 添加一個(gè)SimpleAdapter,其中第三個(gè)參數(shù)直接利用了android中提供的一個(gè)顯示在一列中顯示兩個(gè)TextView的布局,第五個(gè)參數(shù)是android提供的TextView的id
SimpleAdapter adapter = new SimpleAdapter(this, items,
android.R.layout.simple_list_item_2, new String[] {
"user_name", "user_phone" }, new int[] {
android.R.id.text1, android.R.id.text2 });
// 為L(zhǎng)istActivity設(shè)置Adapter
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this, id +":"+((HashMap)getListView().getItemAtPosition(position)).get("user_name").toString(), Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
}
}
能力有限,先寫(xiě)到這里了。
另外可以參看文章:
http://blog.csdn.net/xlfb8057/archive/2008/09/04/2880347.aspx