ListActivity:
一:相關概念簡介(翻譯自官方文檔的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是一個用于通過綁定到諸如array和Cursor等data source并陳列一系列item,當用戶選中一個item的時候,會傳出event handler。
ListActivity擁有一個可以綁定到data source(尤其指帶有query result的array或Cursor)的ListView對象。 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有唯一一個充滿整個screen并位于screen正中的list的默認layout。當然,如果你想的話,你可以用onCreate()里面的setContentView()來透過設置自己的layout來定制這個screen layout。如果你要這樣做,你定制的layout里面務必要包含一個ListView的object,而且它的id必須為“@android:id/list”(括號里面沒看懂什么意思)
另外,你定制的view可以包含另外一個任何種類的view對象,當list view為空的時候它會顯現出來,類似地,它的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每一個單獨的row的layout。當你這樣做時,你可以通過設置ListAdapter對象來決定使用哪個layout resource。一個ListAdapter的constructor至少設置一個parameter來設定layout resource。同時它有著兩個additonal parameter讓你制定哪些data field應該對應到哪個layout resource中的對象中去。這兩個parameter通常是parallel array(我理解成一一對應的兩個數組,比如說key-value pair就是一種parallel array,具體參看下面樣例代碼中的SimpleAdapter的定義過程,共五個參數,其中第三個時上面說的哪個設定layout resource的參數,最后兩個就是那對parallel array了)
android提供了一些標準的row layout resource。他們在R.layout類里面,比如說:
Simple_list_item_1 每項有一個TextView
Simple_list_item_2 每項有兩個TextView
Simple_list_item_checked 帶CheckView的項
Simple_list_item_multiple_choise 每項有一個TextView并可以多選
Simple_list_item_single_choice 每項有一個TextView,但只能進行單選。
(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.
你需要通過一個implement了ListAdapter接口的類來把ListActivity的ListView object綁定到data上。Android提供了兩個標準的list adapters:針對static data(maps)使用的Simple Adapter和針對Cursor query result使用的SimpleCursorAdapter。
二,小零碎:
android:drawSelectorOnTop 默認為false,而且為true的時候顏色會把選項菜單中的字給蓋住。(原文是:When set to true, the selector will be drawn over the selected item. Otherwise the selector is drawn behind the selected item.)
三,個人的理解和樣例代碼:
簡單來說,ListActivity就是一個捆綁了List的Activity,但是他比普通的Activity上外加一個List的建立過程要簡單得多。
普通的Activity要添加List,首先要在layout文件中定義一個ListView,但ListActivity里面則可以連setContentView()都不用寫,Android系統會自動給你分配一個占據全屏幕的List對象。另外,在設置ListAdapter的時候,系統提供了一些默認的布局(比如simple_list_item_2等),可以省去自定義List布局的功夫(當然有必要的時候也是可以自定義List的布局的)。
總體來說,ListActivity就是為了簡化List建立過程而提供的一個Activity的子類。
下面是我自己測試的代碼。(沒有layout文件因為用不上~)
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);
// 定義存放列表的數據結構
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>();
// 為每個數據項填充數據
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****");
// 把數據項添加到列表當中
items.add(user1);
items.add(user2);
items.add(user3);
// 添加一個SimpleAdapter,其中第三個參數直接利用了android中提供的一個顯示在一列中顯示兩個TextView的布局,第五個參數是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 });
// 為ListActivity設置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);
}
}
能力有限,先寫到這里了。
另外可以參看文章:
http://blog.csdn.net/xlfb8057/archive/2008/09/04/2880347.aspx