The adapter object derives from Android.widget.Adapter, which includes constructing the list item control and binding the data item to the list item control.
Common adapters are: Array adapter arrayadapter, database adapter CursorAdapter.
An example of using an adapter object is as follows:
Defining data
String [] data = new string[] {"Good Morning", "Nice Afternoon", "Good Evening"};
Define the adapter and set the data source
Arrayadapter<string>adapter = new arrayadapter<string> (this,data);
To bind an adapter to a list view
Final ListView list = Getlistview ();
List.setadapter (adapter);
The above example can only meet the simple system requirements, in most cases, we need to define the adapter object suitable for list display custom adapters are implemented by deriving adapter and its subclasses, such as:
public class Customadapter extends baseadapter{
Data structure of an item
public int icon;
Public String title;
} ;
Private context context;
Private list<listitem> items;
Public Customadapter (Context context,list<listitem> items)
{
This.context = context;
This.items = items;
}
public int GetCount () {
return Items.size ();
}
Public Object getItems (int position) {
return Items.get (position);
}
public long getitemid (int position) {
return position;
}
Public Veie GetView (int position,convertview,viewgroup parent) {
Constructing a list item control
View view = Createlistitemview ();
Binding Data
ListItem item = items.get (position);
((ImageView) View.findviewbyid (R.id.icon)). Setimageresource (Item.icon);
((TEXTVEIW) View.findviewbyid (R.id.title)). SetText (Item.title);
Return.view;
}
Private View Createlistitemview () {
Layoutinflater Inflater = layoutinflater.from (context);
Return Inflater.inflate (R.layout.list_item,this,null);
}
Adapter Control-adapter