`
myeasyeye
  • 浏览: 16510 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

关于ListActivity

 
阅读更多
public class ListActivity extends Activity

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.

通过绑定到数据源来展示数据的一个Activity,它的数据源来自数组或者游标,同时也会提供当用户选中某个items的处理事件函数。它继承于Activity,很多Activity中的方法它都能用。

ListActivity hosts a ListViewobject 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寄主一个ListView对象,ListView对象可以绑定不同的数据源,典型的就是一个数组或者是一个显示查询结果的游标Cursor。Binding, screen layout, and row layout将会在后面的部分讨论。

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" (orlistif it's in code)

ListActivity 有一个默认的布局包含一个单独的,全屏的List在屏幕中央。不过你可以根据自己的想法,通过setContentView()方法设置你自己的布局文件来自定义屏幕布局。要这样做的话,你自己的视图必须包含一个ListView对象,这个ListView对象的ID必须是"@android:id/list" 的形式,(如果是在代码里就直接是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:id/empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

视情况而定,你定制的view可以包含另一个任何类型的view对象,当list视图为空的时候就会展示出来。这个空的列表的通知者必须有一个空的ID“android:id/empty”。注意的是当空的视图呈现的时候,而且没有要显示的数据,list的视图将会被隐藏。

The following code demonstrates an (ugly) custom screen layout. It has a list with a green background, and an alternate red "no data" message.

下面这段代码是一个很简单的定制布局文件。它的列表是绿色背景,并且是一个交替的红色“没有数据”的信息。

 <?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="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/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).

你可以在列表里面指定特定的行布局。你可以通过在ListAdapter对象里面指定一个布局资源文件,ListAdapter对象寄主在activity里面(ListAdpater把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.

一个ListAdapter 构造器带了一个参数,这个参数指定了一个(针对每一行的)布局资源文件。它也有两个附加的参数,让你指定哪一个数据区域和哪个布局文件中的对象进行关联。这两个参数是典型的并行数组。

Android provides 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 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。

下面这个XML布局文件是two_line_list_item资源文件的代码,展示了两个数据区域,一个在另外一个的上面。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android=http://schemas.android.com/apk/res/android

android:layout_width="match_parent"

android:layout_height="wrap_content"android:orientation="vertical">

<TextViewandroid:id="@+id/text1"android:textSize="16sp"android:textStyle="bold"

android:layout_width="match_parent"android:layout_height="wrap_content"/>

<TextViewandroid:id="@+id/text2"android:textSize="16sp"android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

Binding to Data 绑定数据

You bind the ListActivity's ListView object to data using a class that implements theListAdapter interface. Android provides two standard list adapters:SimpleAdapter for static data (Maps), andSimpleCursorAdapter for Cursor query results.

你把ListActivity的ListView对象与数据绑定,用一个继承ListAdapter接口的类实现。安卓提供了两个标准的listadapter适配器:SimpleAdapter用于静态数据(Map映射),和SimpleCursorAdapter用于游标查询结果。

The following code from a custom ListActivity demonstrates querying the Contacts provider for all contacts, then binding the Name and Company fields to a two line row layout in the activity's ListView。

下面的代码是一个定制的ListActivity范例,查询出所有的联系人,然后绑定姓名和公司地址到一个两行两列的布局文件在Activity的ListView中。

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);
     }
 }
 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ListAdapter

public interface ListAdapter implementsAdapter
android.widget.ListAdapter

Class Overview

Extended Adapter that is the bridge between aListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.

ListAdapter继承于Adapter,是ListView和其支持list的数据的桥梁。数据经常来自于一个cursor,但并不一定是这样要求的。ListView可以显示任何由ListAdapter提供的数据。

也就是说,要让一个listview显示出来需要三个条件:

1. ListView (需要被显示的列表)。

2. Data, 与ListView绑定的数据,一般是一个Cursor或者一个字符串数组。

3. ListAdapter,是data和ListView的桥梁,起一个适配器的作用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics