关于ListActivity的一些总结

ListActivity

一:相关概念简介(翻译自官方文档的classoverview

Anactivity that displays a list of items by binding to a data sourcesuch as an array or Cursor, and exposes event handlers when the userselects an item.

ListActivityhosts 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 followingsections.

ListActivity是一个用于通过绑定到诸如arrayCursordatasource并陈列一系列item,当用户选中一个item的时候,会传出eventhandler

ListActivity拥有一个可以绑定到datasource(尤其指带有queryresultarrayCursor)的ListView对象。Binding,screen layoutrow在下面讨论。

1screenlayout

ListActivityhas a default layout that consists of a single, full-screen list inthe center of the screen. However, if you desire, you can customizethe screen layout by setting your own view layout withsetContentView() in onCreate(). To do this, your own view MUSTcontain 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 todisplay when the list view is empty. This /"empty list/"notifier must have an id /"android:empty/". Note that when anempty view is present, the list view will be hidden when there is nodata to display.

ListActivity有唯一一个充满整个screen并位于screen正中的list的默认layout。当然,如果你想的话,你可以用onCreate()里面的setContentView()来透过设置自己的layout来定制这个screenlayout。如果你要这样做,你定制的layout里面务必要包含一个ListViewobject,而且它的id必须为“@android:id/list”(括号里面没看懂什么意思)

另外,你定制的view可以包含另外一个任何种类的view对象,当listview为空的时候它会显现出来,类似地,它的id必须为“@id/android:empty”

2rowlayout

Youcan specify the layout of individual rows in the list. You do this byspecifying a layout resource in the ListAdapter object hosted by theactivity (the ListAdapter binds the ListView to the data; more onthis later).

AListAdapter constructor takes a parameter that specifies a layoutresource for each row. It also has two additional parameters that letyou specify which data field to associate with which object in therow layout resource. These two parameters are typically parallelarrays.

Androidprovides some standard row layout resources. These are in theR.layout class, and have names such as simple_list_item_1,simple_list_item_2, and two_line_list_item. The following layout XMLis the source for the resource two_line_list_item, which displays twodata fields,one above the other, for each list row.

你可以制定list每一个单独的rowlayout。当你这样做时,你可以通过设置ListAdapter对象来决定使用哪个layoutresource。一个ListAdapterconstructor至少设置一个parameter来设定layoutresource。同时它有着两个additonalparameter让你制定哪些datafield应该对应到哪个layoutresource中的对象中去。这两个parameter通常是parallelarray(我理解成一一对应的两个数组,比如说key-valuepair就是一种parallelarray,具体参看下面样例代码中的SimpleAdapter的定义过程,共五个参数,其中第三个时上面说的哪个设定layoutresource的参数,最后两个就是那对parallelarray了)

android提供了一些标准的rowlayout resource。他们在R.layout类里面,比如说:

Simple_list_item_1每项有一个TextView

Simple_list_item_2每项有两个TextView

Simple_list_item_checkedCheckView的项

Simple_list_item_multiple_choise每项有一个TextView并可以多选

Simple_list_item_single_choice每项有一个TextView,但只能进行单选。

3Bindingto data

Youbind the ListActivity/'s ListView object to data using a class thatimplements the [url=]ListAdapter[/url]interface. Android provides two standard list adapters: [url=]SimpleAdapter[/url]for static data (Maps), and [url=]SimpleCursorAdapter[/url]for Cursor query results.

你需要通过一个implementListAdapter接口的类来把ListActivityListViewobject绑定到data上。Android提供了两个标准的listadapters:针对staticdata(maps)使用的SimpleAdapter和针对Cursor queryresult使用的SimpleCursorAdapter

二,小零碎:

android:drawSelectorOnTop默认为false,而且为true的时候颜色会把选项菜单中的字给盖住。(原文是:Whenset to true, the selector will be drawn over the selected item.Otherwise the selector is drawn behind the selected item.


三,个人的理解和样例代码:

简单来说,ListActivity就是一个捆绑了ListActivity,但是他比普通的Activity上外加一个List的建立过程要简单得多。

普通的Activity要添加List,首先要在layout文件中定义一个ListView,但ListActivity里面则可以连setContentView()都不用写,Android系统会自动给你分配一个占据全屏幕的List对象。另外,在设置ListAdapter的时候,系统提供了一些默认的布局(比如simple_list_item_2等),可以省去自定义List布局的功夫(当然有必要的时候也是可以自定义List的布局的)。

总体来说,ListActivity就是为了简化List建立过程而提供的一个Activity的子类。

下面是我自己测试的代码。(没有layout文件因为用不上~)

 

  1. package level0.test.listActivity;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import android.app.ListActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ListView;
  9. import android.widget.SimpleAdapter;
  10. import android.widget.Toast;
  11. public class ListActivityTest extends ListActivity {
  12.         /** Called when the activity is first created. */
  13.         @Override
  14.         public void onCreate(Bundle savedInstanceState) {
  15.                 super.onCreate(savedInstanceState);
  16.                 // 使用了ListActivity自带的的布局,可以注释掉setContentView()
  17.                 // setContentView(R.layout.main);
  18.                 // 定义存放列表的数据结构
  19.                 List<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
  20.                 HashMap<String, String> user1 = new HashMap<String, String>();
  21.                 HashMap<String, String> user2 = new HashMap<String, String>();
  22.                 HashMap<String, String> user3 = new HashMap<String, String>();
  23.                 // 为每个数据项填充数据
  24.                 user1.put("user_name", "level0");
  25.                 user1.put("user_phone", "135******");
  26.                 user2.put("user_name", "lisi");
  27.                 user2.put("user_phone", "1356*****");
  28.                 user3.put("user_name", "wangwu");
  29.                 user3.put("user_phone", "13544****");
  30.                 // 把数据项添加到列表当中
  31.                 items.add(user1);
  32.                 items.add(user2);
  33.                 items.add(user3);
  34.                 // 添加一个SimpleAdapter,其中第三个参数直接利用了android中提供的一个显示在一列中显示两个TextView的布局,第五个参数是android提供的TextView的id
  35.                 SimpleAdapter adapter = new SimpleAdapter(this, items,
  36.                                 android.R.layout.simple_list_item_2, new String[] {
  37.                                                 "user_name", "user_phone" }, new int[] {
  38.                                                 android.R.id.text1, android.R.id.text2 });
  39.                 // 为ListActivity设置Adapter
  40.                 setListAdapter(adapter);
  41.         }
  42.         @Override
  43.         protected void onListItemClick(ListView l, View v, int position, long id) {
  44.                 // TODO Auto-generated method stub
  45.                 Toast.makeText(this, id +":"+((HashMap)getListView().getItemAtPosition(position)).get("user_name").toString(), Toast.LENGTH_SHORT).show();
  46.                 super.onListItemClick(l, v, position, id);
  47.         }
  48. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值