惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
F
Full Disclosure
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
C
Cisco Blogs
M
MIT News - Artificial intelligence
T
Tenable Blog
G
GRAHAM CLULEY
月光博客
月光博客
Recent Announcements
Recent Announcements
V
Visual Studio Blog
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy International News Feed
P
Proofpoint News Feed
I
Intezer
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
The Cloudflare Blog
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog RSS Feed

博客园 - 蓝风笨笨

简化问题的模式 模板模式和策略模式 为什么要优先使用组合而不是继承 SequenceFile介绍 基于Hadoop Sequencefile的小文件解决方案 Hadoop命令解释 Hadoop源代码分析之Configuration - 蓝风笨笨 onSaveInstanceState和onRestoreInstanceState触发的时机 Android 获取AndroidManifest.xml文件versionCode,versionName属性 . 术语解释 Windows环境下Android Sdk源码下载 Android开发指南中文版(十三)User Interface-Notifications Android开发指南中文版(十二)User Interface-Dialogs Serializable 和 Parcelable 区别 Android开发指南中文版(十一)User Interface-Menus Hierarchy Viewer Tool Android开发指南中文版(十)User Interface-Input Events Android开发指南中文版(九)User Interface-XML Layouts Android开发指南中文版(八)User Interface
Android开发指南中文版(十四)User Interface-Binding to Data with AdapterView
蓝风笨笨 · 2012-01-13 · via 博客园 - 蓝风笨笨

AdapterView是ViewGroup的子类,他的子View由一个绑定了某种数据类型的适配器(Adapter)来决定。当你需要在你的布局中显示存储的数据(不同于资源字符串或绘图资源)时,AdapterView 就非常有用。

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

AdapterView 对象有两个主要职责:

  • 用数据填充布局
  • 处理用户的选取

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

把数据插入布局的典型做法是把AdapterView类绑定到一个适配器(Adapter)上,适配器会从外部源(可能是代码中的一个list或者设备数据库的查询结果)检索数据。

下面的示例代码做如下事情:

  1. 同一个已经存在的View一起,创建一个 Spinner对象, 并且把它绑定到一个新的ArrayAdapter 对象上, 该适配器对象从本地资源中读取一组颜色值。
  2. 从一个View中创建另外一个 Spinner 对象,并且把它绑定到一个新的SimpleCursorAdapter 对象上,该适配器将从设备中上联系人中读取人名(参看Contacts.People章节)。
// Get a Spinner and bind it to an ArrayAdapter that 
// references a String array.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.colors, 
                                         android.R.layout.simple_spinner_item);
adapter.setDropDownViewResourc(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
// Load a Spinner and bind it to a data query.
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...
    new int[] {android.R.id.text1}); // The "text1" view defined in the XML template
adapter2.setDropDownViewResourc(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter2);

注意,在此必须含有People._ID字段,该字段同 CursorAdapter 处于映射使用中,否则你将会得到一个异常。

如果,在你的应用的生命周期中,你变更了正在被你的适配器读取的数据,你应该调用notifyDataSetChanged()方法。该方法将会通知附属的View,其数据已经改变并且他们需要更新自己。

处理用户的选取(Handling User Selections)

要处理用户的选取事件,可以通过设置类的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.
mHistoryView =(ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);