ListActivity简介

本文介绍了如何在Android中使用ListActivity,包括其基本概念、屏幕布局、数据绑定及自定义实现。通过实例展示了如何查询联系人数据并将其展示在一个两行列表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
public class

ListActivity

extends Activity
java.lang.Object
   ↳android.content.Context
    ↳android.content.ContextWrapper
     ↳android.view.ContextThemeWrapper
      ↳android.app.Activity
       ↳android.app.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>  

 

行布局(Row Layout)

你能够指定列表中一个单独的行的布局。只要在ListAdapter对象中指定一个布局资源就可以了。ListAdapter绑定数据到ListView。

一个ListAdapter构造函数有一个参数来指定每一行的布局资源。此外,它还有另外两个参数来指定哪一个数据域与行布局资源中的对象相关联。这两个参数一般是平行数组。

Android 提供了一些标准的布局资源。这些都在R.layout类中,名字诸如simple_list_item_1, simple_list_item_2, 和two_line_list_item. 下面的布局XML是two_line_list_item, 对于每一行,它分两行来表示数据,一个在上一个在下。

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:layout_width="match_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:orientation="vertical">  
  6.   
  7.      <TextView android:id="@+id/text1"  
  8.          android:textSize="16sp"  
  9.          android:textStyle="bold"  
  10.          android:layout_width="match_parent"  
  11.          android:layout_height="wrap_content"/>  
  12.   
  13.      <TextView android:id="@+id/text2"  
  14.          android:textSize="16sp"  
  15.          android:layout_width="match_parent"  
  16.          android:layout_height="wrap_content"/>  
  17.  </LinearLayout>  

你必须确定绑定到这个布局的每一个TextView对象的数据。下一节就做这一方面的介绍。

 

绑定数据

绑定ListActivity的ListView对象和数据,要使用一个实现了ListAdapter接口的类。Android提供了两个标准的list adapters:绑定静态数据(Maps)的SimpleAdapter,和绑定Cursor的SimpleCursorAdapter。

下面这个例子实例一个自定义的ListActivity,它查询Contacts provider的所有contacts,然后绑定Name和Company两个域到ListActivity的ListView中的分为两行的一个列表项。

  1. public class MyListAdapter extends ListActivity {  
  2. @Override  
  3.      protected void onCreate(Bundle savedInstanceState)  
  4.      {  
  5.           super.onCreate(savedInstanceState);           
  6.           // We'll define a custom screen layout here (the one shown above), but           
  7.           // typically, you could just use the standard ListActivity layout.  
  8.           setContentView(R.layout.custom_list_activity_view);   
  9.           // Query for all people contacts using the Contacts.People convenience class.   
  10.           // Put a managed wrapper around the retrieved cursor so we don't have to worry about  
  11.           // requerying or closing it as the activity changes state.   
  12.           mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);   
  13.           startManagingCursor(mCursor);  
  14.           // Now create a new list adapter bound to the cursor.   
  15.           // SimpleListAdapter is designed for binding to a Cursor.  
  16.           ListAdapter adapter = new SimpleCursorAdapter(  
  17.                  this, // Context.  
  18.                  android.R.layout.two_line_list_item,  
  19.           // Specify the row template to use (here, two columns bound to the two retrieved cursor rows).  
  20.                  mCursor,  
  21.           // Pass in the cursor to bind to.  
  22.                  new String[] {People.NAME, People.COMPANY},   
  23.           // Array of cursor columns to bind to.   
  24.                 new int[] {android.R.id.text1, android.R.id.text2});   
  25.           // Parallel array of which template objects to bind to those columns.   
  26.           // Bind to our new adapter.   
  27.           setListAdapter(adapter);   
  28.     }   
  29. }  
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值