The examples in this article describe common adapters and custom adapter usage in Android programming. Share to everyone for your reference, specific as follows:
One, adapter.
As the name suggests, is to make some data appropriate, suitable for easy to display on the view. Can be seen as an understanding of interface data binding. The data that it manipulates is usually some more complicated data, such as group, list, database, collection, etc. Adapters are like monitors, which show complex things in a way that is acceptable to them.
So how does the adapter handle the resulting data and show it out? In fact it is very simple, white adapter It is also a class, within the class it implements these methods of the parent class:
Publicint GetCount ()//Get the number of rows
of data public Object getitem (int position)//the record for a row is public long position according to Getitemid
( int position)//the ID of a record
//The following method is most important compared to several other methods, it explicitly defines how the adapter is going to
display the data we are filling in. In a custom adapter we usually write a layout file to it
publicview getview (int position, View Convertview, ViewGroup parent)
We use a total of three adapters, of course, do not include a custom adapter, which three, I want to use the people know
That is arrayadapter,simpleadapter,simplecursoradapter these three, they all inherit from Baseadapter.
Second, generally for the first two adapters, their data source is nothing more than string[] or list. Below we list two examples of one child:
Example one, the array as a data source, populated by the Arrayadapter
public class Example extends listactivity{
string[] sex = new String () {"Male", "female"}//data source
arrayadapter<string > adapter;//array adapter with the generic public
voidoncreate (Bundle savedinstancestate) {
super.oncreate ( SAVEDINSTANCESTAT);
When the adapter is initialized, and the data source is loaded into the adapter, the
idea of//this.android.r.layout.simple_list_item_1
is to put the data source in a system-defined style into the adapter.
Adapter=newarrayadapter<string> (this.android.r.layout.simple_list_item_1,sex);
This.setadapter (adapter);//This is a control class, so you can bind the adapter directly to the object itself.
}
}
Example two: list as a data source, populated by the Simpleadapter
ListView list = (ListView) Findviewbyid (R.id.mylistview);
Generates a dynamic array and reproduces the data
arraylist
Third, should say two examples are not difficult, are some of the usage we often see, then for Simplecursoradapter how to use it, simplecursoradapter generally mainly used for database, Its data sources are generally the cursor of database queries. Let's look at the following example:
String uristring = "content://contacts/people/";
Cursor mycursor =managedquery (Uri.parse (uristring), NULL, NULL, NULL, NULL);
string[] Fromcolumns = new String[]{people.number, people.name};
int[] Tolayoutids = new int[] {r.id.nametextview, r.id.numbertextview};
Simplecursoradapter Myadapter;
Myadapter=newsimplecursoradapter (This,r.layout.simplecursorlayout,mycursor,fromcolumns,
toLayoutIDs); Pass in the current context, a layout resource, a cursor and two arrays: a name containing the column
//, and another (of the same size) array containing the resource ID in view to display the number of values for the corresponding column
.
Mylistview.setadapter (Myadapter);
We bind a cursor to the ListView and use a custom layout display to display each item.
Four, below we define our own adapter.
Why do you want to define your own adapters, because when we want to use some other way of showing, or we need to, the presentation, this is a DIY.
First we define a class to inherit from Baseadapter, and then let it implement a few of the methods described inside. Then this custom adapter is fine.
There are some of the methods I have introduced above, in this is not to repeat.
public class Imageadapter Extendsbaseadapter {
private context mcontext;
};
There are two parameters in the constructor, one is the source of the data and the other is the context. Public
Imageadapter (integer[] Imgids,context c) {
mcontext=c;
imageids=imgids;
}
Publicint GetCount () {
//TODO auto-generated method stub return
imageids.length;
}
Publicobject getitem (int position) {
//TODO auto-generated method stub return
null;
}
Publiclong getitemid (int position) {
//TODO auto-generated method stub return
position;
}
The main job is to do here, can customize the layout, where I don't say more
publicview getview (int position, View Convertview, ViewGroup parent) {
//TODO auto-generated method Stub
ImageView ImageView = Newimageview (mcontext);
Imageview.setimageresource (Imageids[position]);
Imageview.setlayoutparams (Newgallery.layoutparams (120,120));
Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
Return ImageView
}
}
Finally, this adapter can be used.
I hope this article will help you with the Android program.