AdapterView
是一个 ViewGroup
,它显示加载到适配器中的项目。最常见的适配器类型来自基于数组的数据源。
本指南展示了如何完成与设置适配器相关的几个关键步骤。
用数据填充布局
要将数据添加到您在应用的 UI 中创建的布局中,请添加类似于以下内容的代码
Kotlin
val PROJECTION = arrayOf(Contacts.People._ID, People.NAME) ... // Get a Spinner and bind it to an ArrayAdapter that // references a String array. val spinner1: Spinner = findViewById(R.id.spinner1) val adapter1 = ArrayAdapter.createFromResource( this, R.array.colors, android.R.layout.simple_spinner_item) adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) spinner1.adapter = adapter1 // Load a Spinner and bind it to a data query. val spinner2: Spinner = findViewById(R.id.spinner2) val cursor: Cursor = managedQuery(People.CONTENT_URI, PROJECTION, null, null, null) val adapter2 = SimpleCursorAdapter(this, // Use a template that displays a text view android.R.layout.simple_spinner_item, // Give the cursor to the list adapter cursor, // Map the NAME column in the people database to... arrayOf(People.NAME), // The "text1" view defined in the XML template intArrayOf(android.R.id.text1)) adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) spinner2.adapter = adapter2
Java
// 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.setDropDownViewResource(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.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s2.setAdapter(adapter2);
请注意,在使用 CursorAdapter 时,投影中必须包含 People._ID 列,否则您将收到异常。
如果在应用生命周期中,您更改了适配器读取的底层数据,则应调用 notifyDataSetChanged()
。这将通知附加的视图数据已更改,它应该刷新自身。
注意:使用 Android Studio 3.6 及更高版本,视图绑定 功能可以替换 findViewById()
调用,并为与视图交互的代码提供编译时类型安全性。考虑使用视图绑定而不是 findViewById()
。
处理用户选择
您可以通过将类的 AdapterView.OnItemClickListener
成员设置为侦听器并捕获选择更改来处理用户的选择。
Kotlin
val historyView: ListView = findViewById(R.id.history) historyView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> Toast.makeText(context, "You've got an event", Toast.LENGTH_SHORT).show() }
Java
// Create a message handling object as an anonymous class. private OnItemClickListener messageClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { // Display a messagebox. Toast.makeText(context,"You've got an event",Toast.LENGTH_SHORT).show(); } }; // Now hook into our object and set its onItemClickListener member // to our class handler object. historyView = (ListView)findViewById(R.id.history); historyView.setOnItemClickListener(messageClickedHandler);
有关更多讨论,请参阅 Spinner 主题。