blob: 26364ee8e4c9427a0fe771b182492b542a58ed1c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001page.title=Binding to Data with 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
Scott Mainec80d7f2010-09-24 16:17:27 -070014 <h2>Related tutorials</h2>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015 <ol>
Scott Mainec80d7f2010-09-24 16:17:27 -070016 <li><a href="{@docRoot}resources/tutorials/views/hello-spinner.html">Spinner</a></li>
17 <li><a href="{@docRoot}resources/tutorials/views/hello-listview.html">List View</a></li>
18 <li><a href="{@docRoot}resources/tutorials/views/hello-gridview.html">Grid View</a></li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019 </ol>
20</div>
21</div>
22
23<p>The {@link android.widget.AdapterView} is a ViewGroup subclass whose child Views are determined by an {@link android.widget.Adapter Adapter} that
24binds 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.</p>
25
26<p>{@link android.widget.Gallery Gallery}, {@link android.widget.ListView ListView}, and {@link android.widget.Spinner 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. </p>
27
28
29<p>AdapterView objects have two main responsibilities: </p>
30<ul>
31 <li>Filling the layout with data
32 </li>
33 <li>Handling user selections
34 </li>
35</ul>
36
37
38<h2 id="FillingTheLayout">Filling the Layout with Data</h2>
39<p>Inserting data into the layout is typically done by binding the AdapterView class to an {@link
Elliott Hughes7f877062009-07-30 17:00:34 -070040android.widget.Adapter}, which retrieves data from an external source (perhaps a list that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041the code supplies or query results from the device's database). </p>
42<p>The following code sample does the following:</p>
43<ol>
44 <li>Creates a {@link android.widget.Spinner Spinner} with an existing View and binds it to a new ArrayAdapter
45that reads an array of colors from the local resources.</li>
46 <li>Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read
47people's names from the device contacts (see {@link android.provider.Contacts.People}).</li>
48</ol>
49
50<pre>
51// Get a Spinner and bind it to an ArrayAdapter that
52// references a String array.
53Spinner s1 = (Spinner) findViewById(R.id.spinner1);
54ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
55 this, R.array.colors, android.R.layout.simple_spinner_item);
56adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
57s1.setAdapter(adapter);
58
59// Load a Spinner and bind it to a data query.
60private static String[] PROJECTION = new String[] {
61 People._ID, People.NAME
62 };
63
64Spinner s2 = (Spinner) findViewById(R.id.spinner2);
65Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);
66
67SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
68 android.R.layout.simple_spinner_item, // Use a template
69 // that displays a
70 // text view
Elliott Hughes7f877062009-07-30 17:00:34 -070071 cur, // Give the cursor to the list adapter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 new String[] {People.NAME}, // Map the NAME column in the
73 // people database to...
74 new int[] {android.R.id.text1}); // The "text1" view defined in
75 // the XML template
76
77adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
78s2.setAdapter(adapter2);
79</pre>
80
81<p>Note that it is necessary to have the People._ID column in projection used with CursorAdapter
82or else you will get an exception.</p>
83
84<p>If, during the course of your application's life, you change the underlying data that is read by your Adapter,
85you should call {@link android.widget.ArrayAdapter#notifyDataSetChanged()}. This will notify the attached View
86that the data has been changed and it should refresh itself.</p>
87
88<h2 id="HandlingUserSelections">Handling User Selections</h2>
Elliott Hughes7f877062009-07-30 17:00:34 -070089<p>You handle the user's selection by setting the class's {@link
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090android.widget.AdapterView.OnItemClickListener} member to a listener and
91catching the selection changes. </p>
92<pre>
93// Create a message handling object as an anonymous class.
94private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
95 public void onItemClick(AdapterView parent, View v, int position, long id)
96 {
97 // Display a messagebox.
98 Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();
99 }
100};
101
102// Now hook into our object and set its onItemClickListener member
103// to our class handler object.
104mHistoryView = (ListView)findViewById(R.id.history);
105mHistoryView.setOnItemClickListener(mMessageClickedHandler);
106</pre>
107
108<div class="special">
109<p>For more discussion on how to create different AdapterViews, read the following tutorials:
Dirk Dougherty22558d02009-12-10 16:25:06 -0800110<a href="{@docRoot}resources/tutorials/views/hello-spinner.html">Hello Spinner</a>,
111<a href="{@docRoot}resources/tutorials/views/hello-listview.html">Hello ListView</a>, and
112<a href="{@docRoot}resources/tutorials/views/hello-gridview.html">Hello GridView</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113</div>