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