diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:44:00 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:44:00 -0800 |
commit | d24b8183b93e781080b2c16c487e60d51c12da31 (patch) | |
tree | fbb89154858984eb8e41556da7e9433040d55cd4 /docs/html/guide/topics/ui/binding.jd | |
parent | f1e484acb594a726fb57ad0ae4cfe902c7f35858 (diff) | |
download | frameworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.zip frameworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.tar.gz frameworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.tar.bz2 |
auto import from //branches/cupcake/...@130745
Diffstat (limited to 'docs/html/guide/topics/ui/binding.jd')
-rw-r--r-- | docs/html/guide/topics/ui/binding.jd | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/docs/html/guide/topics/ui/binding.jd b/docs/html/guide/topics/ui/binding.jd new file mode 100644 index 0000000..f9afbc5 --- /dev/null +++ b/docs/html/guide/topics/ui/binding.jd @@ -0,0 +1,110 @@ +page.title=Binding to Data with AdapterView +parent.title=User Interface +parent.link=index.html +@jd:body + +<div id="qv-wrapper"> +<div id="qv"> + <h2>In this document</h2> + <ol> + <li><a href="#FillingTheLayout">Filling the Layout with Data</a></li> + <li><a href="#HandlingUserSelections">Handling User Selections</a></li> + </ol> + + <h2>See also</h2> + <ol> + <li><a href="{@docRoot}guide/tutorials/views/hello-spinner.html">Hello Spinner tutorial</a></li> + <li><a href="{@docRoot}guide/tutorials/views/hello-listview.html">Hello ListView tutorial</a></li> + <li><a href="{@docRoot}guide/tutorials/views/hello-gridview.html">Hello GridView tutorial</a></li> + </ol> +</div> +</div> + +<p>The {@link android.widget.AdapterView} is a ViewGroup subclass whose child Views are determined by an {@link android.widget.Adapter Adapter} that +binds 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> + +<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> + + +<p>AdapterView objects have two main responsibilities: </p> +<ul> + <li>Filling the layout with data + </li> + <li>Handling user selections + </li> +</ul> + + +<h2 id="FillingTheLayout">Filling the Layout with Data</h2> +<p>Inserting data into the layout is typically done by binding the AdapterView class to an {@link +android.widget.Adapter}, which retireves data from an external source (perhaps a list that +the code supplies or query results from the device's database). </p> +<p>The following code sample does the following:</p> +<ol> + <li>Creates a {@link android.widget.Spinner Spinner} with an existing View and binds it to a new ArrayAdapter +that reads an array of colors from the local resources.</li> + <li>Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read +people's names from the device contacts (see {@link android.provider.Contacts.People}).</li> +</ol> + +<pre> +// Get a Spinner and bind it to an ArrayAdapter that +// references a String array. +Spinner s1 = (Spinner) findViewById(R.id.spinner1); +ArrayAdapter<CharSequence> 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 adatper + 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); +</pre> + +<p>Note that it is necessary to have the People._ID column in projection used with CursorAdapter +or else you will get an exception.</p> + + +<h2 id="HandlingUserSelections">Handling User Selections</h2> +<p>You handle the user's selecction by setting the class's {@link +android.widget.AdapterView.OnItemClickListener} member to a listener and +catching the selection changes. </p> +<pre> +// 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); +</pre> + +<div class="special"> +<p>For more discussion on how to create different AdapterViews, read the following tutorials: +<a href="{@docRoot}guide/tutorials/views/hello-spinner.html">Hello Spinner</a>, +<a href="{@docRoot}guide/tutorials/views/hello-listview.html">Hello ListView</a>, and +<a href="{@docRoot}guide/tutorials/views/hello-gridview.html">Hello GridView</a>. +</div> |