Android:ListActivity之学习
来源:互联网 发布:javascript廖学峰 编辑:程序博客网 时间:2024/05/01 14:44
ListActivity是Activity的一个子类。
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.
以列表的形式显示数据,它的数据源主要来自数组或者是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)
可以自定义ListView的效果,但是ListView的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文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
当使用系统提供的two_line_list_item这个row layout的时候,两个TextView的id:text1、text2就是可以全局使用了,
所以不要在其他地方定义一样的id,以免发生冲突。
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.
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
extends BaseAdapterimplements 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)
Constructor
- Android:ListActivity之学习
- Android ListActivity学习
- android 之 ListActivity & ListView
- Android之ListActivity
- Android之ListActivity
- Android控件之ListActivity
- 菜鸟Android学习之路10-Activity继承ListActivity实现
- SimpleAdapter和Baseadapter填充listActivity-android学习之旅()
- Android编程学习笔记 之 ListActivity源码详解
- Android列表视图ListView和ListActivity-android学习之旅(二十四)
- Android之ListActivity:布局与数据绑定
- Android之ListActivity 布局与数据绑定
- Android之ListActivity:布局与数据绑定
- Android之ListActivity:布局与数据绑定
- Android之ListActivity:布局与数据绑定
- Android之ListActivity:布局与数据绑定
- android ListActivity
- android listActivity
- JMeter的简单使用
- 关于程序集
- 打的
- vbs 代码 :GetFileName
- 网站项目管理经验
- Android:ListActivity之学习
- 如何理解stdlib.h里的_countof()宏(转)
- LIveWriter中使用插入代码块插件,出现意外错误
- [经典]HTML标签的英文全称与中文释义
- number one
- 多表统计相同字段用 UNION就行——你能想到的人家都想到了
- rman的基础知识和备份的学习
- 证明题 (转自和菜头)
- 百度排名软件,提升网站排名