public class ListActivity extends Activity
 
java.lang.Object
    android.content.Context
      android.content.ContextWrapper
        android.view.ContextThemeWrapper
          android.app.Activity
            android.app.ListActivity
ListActivity_职场Known Direct Subclasses

Class Overview

ListActivity显示一个绑定到数组或游标这些数据源的一个列表,并且列表的每一项提供一个点击事件的管理方法,当用户点击其中的列表项的时候就能进行相应的处理。

ListActivity容纳了一个ListView对象,这个对象能够被绑定到不同的数据源,一般是一个数组或者存储了一组查询结果的游标。

 

屏幕布局

ListActivity的默认布局由一个位于屏幕中心的全屏列表构成。但是,如果你不想使用默认的布局,可以在onCreate()方法中通过setContentView()方法设定你自己定制的布局。

如果指定你自己定制的布局,你的布局中必须包含一个id为"@android:id/list"(如果是使用代码的形式,则是 list)的ListView 。此外,你自定义的view为空时,能够包含另外一个任何类型的view对象。

"empty list"notifier必须有一个id"android:empty"。要注意的是:Note that when an empty view is present, the list view will be hidden when there is no data to display.

下面的代码示例一个丑陋的自定义屏幕布局。这个布局有一个list,这个list有绿色的背景色,还有一个用来代替的红色的“no data”消息。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:orientation="vertical"  
  4.         android:layout_width="match_parent"  
  5.         android:layout_height="match_parent"  
  6.         android:paddingLeft="8dp"  
  7.         android:paddingRight="8dp">  
  8.   
  9.     <ListView android:id="@id/android:list"  
  10.               android:layout_width="match_parent"  
  11.               android:layout_height="match_parent"  
  12.               android:background="#00FF00"  
  13.               android:layout_weight="1"  
  14.               android:drawSelectorOnTop="false"/>  
  15.   
  16.     <TextView id="@id/android:empty"  
  17.               android:layout_width="match_parent"  
  18.               android:layout_height="match_parent"  
  19.               android:background="#FF0000"  
  20.               android:text="No data"/>  
  21. </LinearLayout>