- 浏览: 560512 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (338)
- 已过时文章(留念用) (39)
- Android学习笔记 (30)
- Android开发指引自译 (100)
- Android NDK文档自译 (23)
- Android设计指引自译 (2)
- xp(ペケピー)&linux(理奈、铃)酱~ (4)
- ui酱&歌词自译~ (9)
- lua酱~ (9)
- 自我反省 (1)
- 羽game计划 (1)
- XSL酱 (2)
- java酱 (3)
- 设计的领悟 (58)
- 涂鸦作品(pixiv) (1)
- ruby酱 (2)
- Objective-C编程语言自译 (2)
- Android开发月报 (6)
- objc酱 (2)
- photoshop (3)
- js酱 (6)
- cpp酱 (8)
- antlr酱 (7)
- Lua 5.1参考手册自译 (11)
- 收藏品 (3)
- 待宵草计划 (4)
- 体验版截图 (1)
最新评论
-
naruto60:
太给力了!!!!我这网打不开Intel官网,多亏楼主贴了连接, ...
使用HAXM加速的Android x86模拟器(和一些问题) -
yangyile2011:
谢谢博主,翻译得很好哦
【翻译】(4)片段 -
ggwang:
牙痛的彼岸:痹!
牙痛的彼岸 -
ggwang:
总结得很简练清晰啊,学习了!
ANTLR学习笔记一:概念理解 -
leisurelife1990:
mk sdd
用git下载Android自带app的源代码
【翻译】(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来源许可证描述的条款进行修改)
发表评论
-
【翻译】(9-补丁2)电话簿提供者
2012-07-18 12:54 2334【翻译】(9-补丁2)电话簿提供者 see h ... -
【翻译】(8-补丁1)Android接口定义语言(AIDL)
2012-07-02 05:55 2852【翻译】(8-补丁1)Andro ... -
【翻译】(0)应用组件
2012-06-30 23:50 790【翻译】(0)应用组件 see http:// ... -
【翻译】(88)传感器
2012-05-21 21:25 1025【翻译】(88)传感器 ... -
【翻译】(87)复制与粘贴
2012-05-20 14:48 1887【翻译】(87)复制与粘贴 see http: ... -
【翻译】(86)音频捕捉
2012-05-16 15:14 1039【翻译】(86)音频捕捉 ... -
【翻译】(85)照相机
2012-05-13 15:09 3731【翻译】(85)照相机 see http:// ... -
【翻译】(84)JetPlayer
2012-04-21 16:24 936【翻译】(84)JetPlayer see h ... -
【翻译】(83)媒体回放
2012-04-21 16:00 1802【翻译】(83)媒体回放 see http:/ ... -
【翻译】(82)多媒体与照相机
2012-04-18 23:05 906【翻译】(82)多媒体与照相机 see htt ... -
【翻译】(23-补丁3)构建无障碍服务
2012-04-18 21:57 1584【翻译】(23-补丁3)构 ... -
【翻译】(23-补丁2)使应用程序无障碍
2012-04-16 13:08 2031【翻译】(23-补丁2)使应用程序无障碍 see ... -
【翻译】(23-补丁1)无障碍
2012-04-11 22:38 858【翻译】(23-补丁1)无 ... -
【翻译】(81)Renderscript之运行时API参考手册
2012-04-11 22:13 1380【翻译】(81)Renderscript之运行时API参 ... -
【翻译】(80)Renderscript之计算
2012-04-09 14:09 1393【翻译】(80)Renderscript之计算 ... -
【翻译】(79)Renderscript之图形
2012-04-08 13:59 2802【翻译】(79)Renderscript之图形 ... -
【翻译】(78)Renderscript
2012-04-04 15:35 1394【翻译】(78)Renderscript see ... -
【翻译】(77)可绘画对象动画
2012-03-18 10:52 667【翻译】(77)可绘画对象动画 see htt ... -
【翻译】(76)视图动画
2012-03-18 10:04 761【翻译】(76)视图动画 see http:/ ... -
【翻译】(75)属性动画
2012-03-17 18:24 2476【翻译】(75)属性动画 see http:/ ...
相关推荐
一个AdapterView工具,仿照Android里面的AdapterView,用于Unity中
个人实现了一个比较简单的AdapterView的实现类,希望能够帮助到大家理解AdapterView的实现。
android studio移动开发基础:adapterview PPT
要在AdapterView(ListView、GridView等诸如此类的视图)没有数据时显示自定义的视图。
适配器是一个连接数据和AdapterView(ListView就是一个典型的AdapterView,后面还会学习其他的)的桥梁,通过它能有效地实现数据与AdapterView的分离设置,使AdapterView与数据的绑定更加简便,修改更加方便 ...
在 AdapterView 和 RecyclerView 中通用的 Adapter 和 ViewHolder。RecyclerView 支持 DataBinding 、多种 Item 类型、添加 Header 和 Footer
It supports AdapterView(ListView,GridView,etc) and RecyclerView. Dependency compile 'com.barryzhang:temptyview:0.0.1' Usage I. Initialize initialize some custom attributes —— this step is not ...
适用于Android的可缩放ImageView可以在AdapterView和Recyclerview中使用。一个可拉伸拉伸的ImageView,可在AdapterView和RecyclerView中使用。 特征 使用多点触控进行缩放。 可以在AdapterView和Recyclerview中...
使用它可以用来显示我们常见的列表形式,继承自抽象类 AdapterView。 常用适配器介绍 ArrayAdapter:简单、易用的 Adapter,用于将数组数据作为数据源绑定到列表项中。支持泛型操作 SimpleAdapter:相比 ...
week10-database-sqlite sqlite数据库 使用sqlite数据库的示例代码,可对UI进行动态操作根据需要创建视图以处理游标数据集 ... 使用sqlite数据库的示例代码,UI使用AdapterView,因此sdk类用于创建和操作UI
OverlapImageGalleryView Overview OverlapImageGalleryView is a flexible library which helps you to create overlapping images gallery in your android Application. You can easily integrate it with the ...
在 AdapterView 和 RecyclerView 中通用的 Adapter 和 ViewHolder。 BGAAdapterViewAdapter 和 BGAViewHolderHelper 用于简化 AdapterView 的子类(如 ListView、GridView)的适配器的编写 BGARecyclerViewAdapter...
是否厌倦了前面“平淡无奇”的UI组件学习,感觉功能just soso?没关系,从本集开始,疯兄将给众位带来更加高级别的UI组件,这些高级别的UI组件在关键时候一旦出手,将给芸芸众生带来神一般的体验和惊艳的爽感!...
listview子项(Item)的取值存在很多问题,常常会因为方法不对而取到空值,或者直接抛空指针异常。对于这些问题,也许你曾和我一样困惑,不过,我相信,你看完我的例子之后可以茅塞顿开。为了照顾新手、菜鸟,本人...
spProvince = (Spinner)findViewById(R.id.... //绑定数据到Spinner(City)上 spCity.setAdapter(cityAdapter); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
Adapter对象是AdapterView和底层数据见的桥梁。Adapter用于访问数据项,并且负责为数据项生成视图 AdapterView是一个抽象类,用于那些需要通过Adapter填充自身的视图,其常见子类是ListView。显示AdapterView时会...
4.2.4 将数据绑定到AdapterView 77 4.2.5 创建菜单 79 4.2.6 创建Dialog 81 4.2.7 通知用户 83 4.2.8 处理用户输入 84 4.2.9 样式与主题 84 4.3 常用Widget 86 4.3.1 TextView 86 4.3.2 Button 89 4.3.3 ImageView ...
常用Adapter配合ListView使用实例
Resource Description can be explained, fragments of the document outlines the e-book, the source code to fill in here will be described in detail in our recommendation, more likely to be others to ...
如何避开在ListView等AdapterView上动态添加删除项的陷阱,下面就为大家分享,具体内容如下 首先,定义如下array资源,作为列表的加载内容: <string name=app_name>MyListView <item>Java <item>C <item>...