`

【翻译】(24)用AdapterView绑定到数据

 
阅读更多

 

【翻译】(24)用AdapterView绑定到数据

 

see

http://developer.android.com/guide/topics/ui/binding.html

 

原文见

http://developer.android.com/guide/topics/ui/binding.html

 

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

 

Binding to Data with AdapterView

 

用AdapterView绑定到数据

 

In this document

 

本文目录

 

Filling the Layout with Data 用数据填充布局

Handling User Selections 处理用户选择

 

Related tutorials

 

相关教程

 

Spinner

List View

Grid View

 

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

 

The AdapterView is a ViewGroup subclass whose child Views are determined by an Adapter that binds to data of some type. AdapterView is useful whenever you need to display stored data (as opposed to resource strings or drawables) in your layout.

 

AdapterView是一个ViewGroup子类,它的子视图由一个绑定到一些类型的数据的Adapter决定。无论是你需要何时在你的布局中显示存储的数据(不同于资源字符串或可绘画对象),AdapterView都很有用。

 

Gallery, ListView, and Spinner are examples of AdapterView subclasses that you can use to bind to a specific type of data and display it in a certain way.

 

Gallery,ListView和Spinner是AdapterView子类的示例,你可以使用它们绑定到一个特定类型的数据并且以某种方式显示它。

 

AdapterView objects have two main responsibilities:

 

AdapterView对象拥有两个主要职责:

 

* Filling the layout with data 用数据填充布局

* Handling user selections 处理用户选择

 

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

 

Filling the Layout with Data 

 

用数据填充布局

 

Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).

 

插入数据进布局是典型地通过绑定AdapterView类到一个从外部源(可能是一个代码提供的列表或从设备数据库中查询的结果)中取出数据的Adapter来做到的。

 

The following code sample does the following:

 

以下代码示例做以下事情:

 

1. Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.

 

1. 用一个现存View创建一个Spinner并且绑定它到一个从局部资源中读取颜色数组的新ArrayAdapter。

 

2. Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read people's names from the device contacts (see Contacts.People).

 

2. 从一个View中创建另一个Spinner典型并且绑定它到一个从设备电话簿中读取联系人名称的新SimpleCursorAdapter(见Contacts.People)。

 

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

 

// Get a Spinner and bind it to an ArrayAdapter that 

// references a String array.

// 获取一个Spinner并把它绑定到引用一个String数组的ArrayAdapter。

Spinner s1 = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter adapter = ArrayAdapter.createFromResource(

    this, R.array.colors, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

s1.setAdapter(adapter);

 

// Load a Spinner and bind it to a data query.

// 加载一个Spinner并绑定它到一个数据查询。

private static String[] PROJECTION = new String[] {

        People._ID, People.NAME

    };

 

Spinner s2 = (Spinner) findViewById(R.id.spinner2);

Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);

 

SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,

    android.R.layout.simple_spinner_item, // Use a template

                                          // that displays a

                                          // text view

                                          // 使用一个显示文本视图的模板

    cur, // Give the cursor to the list adapter

         // 把游标传给列表适配器

    new String[] {People.NAME}, // Map the NAME column in the

                                         // people database to...

                                // 映射联系人数据库的NAME列到……

    new int[] {android.R.id.text1}); // The "text1" view defined in

                                     // the XML template

                                     // "text1"视图定义在XML模板中

 

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

s2.setAdapter(adapter2);

 

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

 

Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.

 

注意必须在投影中有People._ID列被CursorAdapter使用,否则你将得到一个异常。

 

If, during the course of your application's life, you change the underlying data that is read by your Adapter, you should call notifyDataSetChanged(). This will notify the attached View that the data has been changed and it should refresh itself.

 

如果,在你的应用程序的生命过程间,你改变被你的Adapter读取的底层数据,那么你应该调用notifyDataSetChanged()。这将通知被依附的View数据已经被改变并且它应该刷新自己。

 

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

 

Handling User Selections

 

处理用户选择

 

You handle the user's selection by setting the class's AdapterView.OnItemClickListener member to a listener and catching the selection changes.

 

你通过设置类的AdapterView.OnItemClickListener成员为一个监听器来处理用户的选择,并且捕获选择的改变。

 

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

 

// Create a message handling object as an anonymous class.

// 创建一个消息处理对象作为一个匿名类。

private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position, long id)

    {

        // Display a messagebox.

        Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();

    }

};

 

// Now hook into our object and set its onItemClickListener member

// to our class handler object.

// 现在挂钩进我们的对象并且设置它的onItemClickListener成员

// 为我们的类处理对象。

mHistoryView = (ListView)findViewById(R.id.history);

mHistoryView.setOnItemClickListener(mMessageClickedHandler); 

 

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

 

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

 

For more discussion on how to create different AdapterViews, read the following tutorials: Hello Spinner, Hello ListView, and Hello GridView.

 

想获取更多关于如何创建不同AdapterViews的讨论,请阅读以下教程:你好Spinner,你好ListView,以及你好GridView。

 

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

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 16 Dec 2011 21:54

 

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

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics