Adapterview is a subclass of viewgroup. Its subview is determined by the adapter class bound to some types of data. Adapterview helps to display stored data (relative to a resource string or can be traced) in the layout as needed ).
Gallery, listview, and spinner are examples of adapterview subclasses. You can use them to bind specific types of data and display it in some way.
Adapterview objects have two main responsibilities:
1. Fill the layout with data
2. process user selection
Data filling Layout
Generally, the adapterview class is bound to an apater class to insert data into the layout. The adapter obtains data from an external resource (possibly a data list provided by the Code, it may also be a query result from the device database ).
The following code demonstrates how to fill data:
1. Create a existing spinner object and bind it to a new arrayadapter object. The arrayadater object reads the color array from the local resource;
2. Create another spinner object and bind it to a simplecursoradapter object. simplecursoradapter reads the name of a person from the contact list of the device.
// 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
// Text View
Cur,
// Give the cursor to the list Adapter
New
String []
{People. name },
// Map the name column in
// People database...
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 );
Note: The cursoradapter object must have a people. _ ID column in field ing; otherwise, an exception occurs.
If the data read by the adapter changes during the survival of the application, the notifydatasetchanged () method should be called to notify the bound view that the data has changed, view should refresh itself.
Process User Selection
By setting the adapterview. onitemcliclistener member of the class to a listener, you can process the user's selection and capture the selection changes. For example:
// 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 );