• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=AdapterView
2parent.title=User Interface
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8  <h2>In this document</h2>
9  <ol>
10    <li><a href="#FillingTheLayout">Filling the Layout with Data</a></li>
11    <li><a href="#HandlingUserSelections">Handling User Selections</a></li>
12  </ol>
13</div>
14</div>
15
16
17
18<pre>
19// Get a Spinner and bind it to an ArrayAdapter that
20// references a String array.
21Spinner s1 = (Spinner) findViewById(R.id.spinner1);
22ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
23    this, R.array.colors, android.R.layout.simple_spinner_item);
24adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
25s1.setAdapter(adapter);
26
27// Load a Spinner and bind it to a data query.
28private static String[] PROJECTION = new String[] {
29        People._ID, People.NAME
30    };
31
32Spinner s2 = (Spinner) findViewById(R.id.spinner2);
33Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);
34
35SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
36    android.R.layout.simple_spinner_item, // Use a template
37                                          // that displays a
38                                          // text view
39    cur, // Give the cursor to the list adapter
40    new String[] {People.NAME}, // Map the NAME column in the
41                                         // people database to...
42    new int[] {android.R.id.text1}); // The "text1" view defined in
43                                     // the XML template
44
45adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
46s2.setAdapter(adapter2);
47</pre>
48
49<p>Note that it is necessary to have the People._ID column in projection used with CursorAdapter
50or else you will get an exception.</p>
51
52<p>If, during the course of your application's life, you change the underlying data that is read by your Adapter,
53you should call {@link android.widget.ArrayAdapter#notifyDataSetChanged()}. This will notify the attached View
54that the data has been changed and it should refresh itself.</p>
55
56<h2 id="HandlingUserSelections">Handling User Selections</h2>
57<p>You handle the user's selection by setting the class's {@link
58android.widget.AdapterView.OnItemClickListener} member to a listener and
59catching the selection changes. </p>
60<pre>
61// Create a message handling object as an anonymous class.
62private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
63    public void onItemClick(AdapterView parent, View v, int position, long id)
64    {
65        // Display a messagebox.
66        Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();
67    }
68};
69
70// Now hook into our object and set its onItemClickListener member
71// to our class handler object.
72mHistoryView = (ListView)findViewById(R.id.history);
73mHistoryView.setOnItemClickListener(mMessageClickedHandler);
74</pre>
75
76<div class="special">
77<p>For more discussion on how to create different AdapterViews, read the following guides:
78<a href="{@docRoot}guide/topics/ui/controls/spinner.html">Spinner</a>,
79<a href="{@docRoot}guide/topics/ui/layout/listview.html">List View</a>, and
80<a href="{@docRoot}guide/topics/ui/layout/gridview.html">Grid View</a>.
81</div>
82