AdapterView是ViewGroup的一个子类,其子类View由一个Adapter来决定与何种类型的数据相绑定。AdapterView在需要在布局中显示储存的数据(与资源字符串或是可绘制内容相对)时是非常有用的。
Gallery、ListView和Spinner是可以以特定方式用来与特殊类型数据绑定显示的AdapterView子类的几个例子。
AdapterView对象有两项主要任务:
- 用数据填充布局
- 处理用户选择
用数据填充布局
通过把AdapterView类与一个Adapter绑定可以将数据插入布局之中,Adapter会从外部资源获取数据(比如在代码中提供的一个列表或是来自设备的数据库中的一个查询结果)。
下面的范例代码的功能是:
- 借助于一个已有的View创建一个Spinner并将其绑定至一个新的ArrarAdapter来从本地资源中读取一列色彩。
- 在一个View中创建另一个Spinner并将其绑定至一个新的SimpleCursorAdapter来从设备通讯录中读取联系人姓名(参见Contacts.People)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span style="color: #993300;">// Get a Spinner and bind it to an ArrayAdapter that </span> <span style="color: #993300;">// references a String array.</span> <span style="color: #000000;"><span style="color: #800080;">Spinner </span>s1 = (<span style="color: #800080;">Spinner</span>) findViewById(R.id.spinner1);</span> <span style="color: #000000;"><span style="color: #800080;">ArrayAdapter </span>adapter = <span style="color: #800080;">ArrayAdapter</span>.createFromResource(</span> <span style="color: #000000;"><span style="color: #000080;"> this</span>, R.array.colors, android.R.layout.simple_spinner_item);</span> <span style="color: #000000;">adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);</span> <span style="color: #000000;">s1.setAdapter(adapter);</span> <span style="color: #993300;">// Load a Spinner and bind it to a data query.</span> <span style="color: #000000;"><span style="color: #000080;">private static </span><span style="color: #800080;">String</span>[] PROJECTION = <span style="color: #000080;">new </span><span style="color: #800080;">String</span>[] {</span> <span style="color: #000000;"><span style="color: #800080;"> People</span>._ID, <span style="color: #800080;">People</span>.NAME</span> <span style="color: #000000;"> };</span> <span style="color: #000000;"><span style="color: #800080;">Spinner </span>s2 = (<span style="color: #800080;">Spinner</span>) findViewById(R.id.spinner2);</span> <span style="color: #000000;"><span style="color: #800080;">Cursor </span>cur = managedQuery(<span style="color: #800080;">People</span>.CONTENT_URI, PROJECTION, <span style="color: #000080;">null</span>, <span style="color: #000080;">null</span>);</span> <span style="color: #000000;"><span style="color: #800080;">SimpleCursorAdapter </span>adapter2 = <span style="color: #000080;">new </span><span style="color: #800080;">SimpleCursorAdapter</span>(<span style="color: #000080;">this</span>,</span> <span style="color: #000000;"> android.R.layout.simple_spinner_item, <span style="color: #993300;">// Use a template</span></span> <span style="color: #993300;"> // that displays a</span> <span style="color: #993300;"> // text view</span> <span style="color: #000000;"> cur, // Give the cursor to the list adapter</span> <span style="color: #000000;"><span style="color: #000080;"> new </span><span style="color: #800080;">String</span>[] {<span style="color: #800080;">People</span>.NAME}, <span style="color: #993300;">// Map the NAME column in the</span></span> <span style="color: #993300;"> // people database to...</span> <span style="color: #000000;"><span style="color: #000080;"> new int</span>[] {android.R.id.text1}); <span style="color: #993300;">// The "text1" view defined in</span></span> <span style="color: #993300;"> // the XML template</span> <span style="color: #000000;">adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);</span> <span style="color: #000000;">s2.setAdapter(adapter2);</span> |
注意必须让People._ID栏通过CursorAdapter投影,不然会收到一个异常。
如果在程序的生命周期内改变了Adapter所读取的数据,那就该调用notifyDataSetChanged()。这将告诉被绑定的View数据已被改变以刷新显示。
处理用户选择
可以通过将类的AdapterView.OnItemClickListener成员与一个监听器(listener)相结合并捕获选择动作来处理用户选择。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #800000;">// Create a message handling object as an anonymous class.</span> <span style="color: #000000;"><span style="color: #000080;">private </span><span style="color: #800080;">OnItemClickListener </span>mMessageClickedHandler = <span style="color: #000080;">new </span><span style="color: #800080;">OnItemClickListener</span>() {</span> <span style="color: #000000;"><span style="color: #000080;"> public void </span>onItemClick(<span style="color: #800080;">AdapterView </span>parent, <span style="color: #800080;">View </span>v, <span style="color: #000080;">int </span>position, <span style="color: #000080;">long </span>id)</span> <span style="color: #000000;"> {</span> <span style="color: #800000;"> // Display a messagebox.</span> <span style="color: #000000;"><span style="color: #800080;"> Toast</span>.makeText(mContext,<span style="color: #008000;">"You've got an event"</span>,<span style="color: #800080;">Toast</span>.LENGTH_SHORT).show();</span> <span style="color: #000000;"> }</span> <span style="color: #000000;">};</span> <span style="color: #800000;">// Now hook into our object and set its onItemClickListener member</span> <span style="color: #800000;">// to our class handler object.</span> <span style="color: #000000;">mHistoryView = (<span style="color: #800080;">ListView</span>)findViewById(R.id.history);</span> <span style="color: #000000;">mHistoryView.setOnItemClickListener(mMessageClickedHandler); </span> |
关于如何创建不同类型的AdapterView的更多内容,请阅读下面的教程:Hello Spinner、Hello ListView以及Hello GridView。
本作品采用知识共享 署名-非商业性使用-禁止演绎 3.0 Unported许可协议进行许可。