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文件因为用不上~)
package level0.test.listActivity;
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
posted on 2010-12-26 10:47
level0 阅读(381)
评论(0) 编辑 收藏 所属分类:
android日志