ListActivity

http://blog.csdn.net/urecvbnkuhbh_54245df/article/details/5797004
ListActivity是Activity的一个子类。

以列表的形式显示数据,它的数据源主要来自数组或者是Cursor,

同时也会提供一些处理用户选择某个item的处理函数,也就是平常所说的回调函数,它本身继承于Activity,所以很多Activity中的方法它都能用。

 

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来显示并绑定数据,需要明确的是screen layout,这个是屏幕上的整体显示布局文件;row layout是ListView中每行显示的布局文件。

 

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)

 

Activity默认是单一,全屏的,

但是你也可以用可以 在onCreate()方法中使用setContentView() 进行自定义界面

自定义ListView的效果,但是ListView的id必须设置为 "@android:id/list" 

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.

 

最好指定一下当没有数据可以显示时,应该显示什么。这是就要用到"@android:id/empty"作为id,这是系统规定的。

 

下面就是就是一个screen layout的例子:

 

 <?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         
android:orientation="vertical"
         
android:layout_width="match_parent"
         
android:layout_height="match_parent"
         
android:paddingLeft="8dp"
         
android:paddingRight="8dp">

     
<ListView android:id="@id/android:list"
               
android:layout_width="match_parent"
               
android:layout_height="match_parent"
               
android:background="#00FF00"
               
android:layout_weight="1"
               
android:drawSelectorOnTop="false"/>

     
<TextView id="@id/android:empty"
               
android:layout_width="match_parent"
               
android:layout_height="match_parent"
               
android:background="#FF0000"
               
android:text="No data"/>
 
</LinearLayout>
 

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).

 

你可以对list中特定的某一行的布局进行设置,ListAdapter将ListView绑定到数据。

 

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.

 

 

Android提供了一些标准的行布局资源,都是在R.layout类中,如:simple_list_item_1, simple_list_item_2, and two_line_list_item。下面是two_line_list_item的实现xml文件:

<span class="pln"> </span><span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln">
 </span><span class="tag"><LinearLayout</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln">
     </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"match_parent"</span><span class="pln">
     </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">
     </span><span class="atn">android:orientation</span><span class="pun">=</span><span class="atv">"vertical"</span><span class="tag">></span><span class="pln">

     </span><span class="tag"><TextView</span><span class="pln"> </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/text1"</span><span class="pln">
         </span><span class="atn">android:textSize</span><span class="pun">=</span><span class="atv">"16sp"</span><span class="pln">
         </span><span class="atn">android:textStyle</span><span class="pun">=</span><span class="atv">"bold"</span><span class="pln">
         </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"match_parent"</span><span class="pln">
         </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="tag">/></span><span class="pln">

     </span><span class="tag"><TextView</span><span class="pln"> </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/text2"</span><span class="pln">
         </span><span class="atn">android:textSize</span><span class="pun">=</span><span class="atv">"16sp"</span><span class="pln">
         </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"match_parent"</span><span class="pln">
         </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="tag">/></span><span class="pln">
 </span><span class="tag"></LinearLayout></span><span class="pln">
 </span>
<span class="pln">当使用系统提供的two_line_list_item这个row layout的时候,两个TextView的id:text1、text2就是可以全局使用了,</span>
<span class="pln">所以不要在其他地方定义一样的id,以免发生冲突。</span>

You must identify the data bound to each TextView object in this layout. The syntax for this is discussed in the next section.

 

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.

 

可以通过使用一个实现ListAdapter接口的类来将ListView对象绑定到数据。Android本身提供了两个标准的list adapter,一个是SimpleAdapter用于静态数据(Maps),另一个是SimpleCursorAdapter用于Cursor查询结果。

 

下面是SDK中的一个例子:

 

 public class MyListAdapter extends ListActivity {

     
@Override
     
protected void onCreate(Bundle savedInstanceState){
         
super.onCreate(savedInstanceState);

         
// We'll define a custom screen layout here (the one shown above), but
         
// typically, you could just use the standard ListActivity layout.
         setContentView
(R.layout.custom_list_activity_view);

         
// Query for all people contacts using the Contacts.People convenience class.
         
// Put a managed wrapper around the retrieved cursor so we don't have to worry about
         
// requerying or closing it as the activity changes state.
         mCursor 
= this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
         startManagingCursor
(mCursor);

         
// Now create a new list adapter bound to the cursor.
         
// SimpleListAdapter is designed for binding to a Cursor.
         
ListAdapter adapter = new SimpleCursorAdapter(
                 
this, // Context.
                 android
.R.layout.two_line_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor
 rows
).
                 mCursor
,                                              // Pass in the cursor to bind to.
                 
new String[] {People.NAME, People.COMPANY},           // Array of cursor columns to bind to.
                 
new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.

         
// Bind to our new adapter.
         setListAdapter
(adapter);
     
}
 
}
 

好了,接下来再来学习一下SimpleCursorAdapter:

An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.

 

SimpleCursorAdapter主要是用来把数据库里面的数据作为数据源显示出来。可以指定显示数据库表中的哪一列,用什么view显示,以及显示时具体的xml文件。

 

看一下它的构造函数旧非常清楚了:

 

 

 

public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

Constructor.

contextThe context where the ListView associated with this SimpleListItemFactory is running
layoutresource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
cThe database cursor. Can be null if the cursor is not available yet.
fromA list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
to

The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.  

 
下面再把SimpleAdapter简单的介绍一下:
 
public class

SimpleAdapter

extends  BaseAdapter
implements  Filterable

Class Overview

An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases.

 

这是一个把静态数据映射到定义在xml文件中的view的简单适配器类。可以把需要显示在ListView中的数据保存成Map类型的ArrayList,在ArrayList中的每条数据对应的是ListView中的一行。

 

看一下构造函数就很清楚了:

 

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Since:  API Level 1
 

Constructor

contextThe context where the View associated with this SimpleAdapter is running
dataA List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resourceResource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
fromA list of column names that will be added to the Map associated with each item.
toThe views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值