Commonly used adapters in Android and define their own adapters

Source: Internet
Author: User

Reprint: http://blog.chinaunix.net/uid-11898547-id-3303153.html

Http://www.tudou.com/home/_328390108/item

One, adapter.
As the name implies, it is appropriate to make some data suitable for display on the view. Can be seen as an understanding of interface data binding. The data it manipulates are generally complex data, such as arrays, lists, databases, collections, and so on. The adapter is like a display that presents complex things in a way that people can accept.

So how does the adapter handle the resulting data and show it out? In fact, it's very simple. Adapter it is also a class, within the class it implements these methods of the parent class:


Publicint GetCount ()//number of rows to get data

Public Object getItem (int position)//Get a record of a row based on position

public long getitemid (int position)//the ID of a record

The following method is most important compared to several other methods, it explicitly defines what the adapter will be

Way to show the data we've populated, and in a custom adapter we'll usually write a layout file for it.

Publicview GetView (int position, View Convertview, ViewGroup parent)

There are three adapters in our common use, of course not including custom adapters, which three, I want to use people know


That is arrayadapter,simpleadapter,simplecursoradapter these three, they are all inherit from Baseadapter.

Second, generally for the first two adapters, their data source is nothing more than string[] or list. Here we list two examples of one child:
Example one, an array as a data source, populated with Arrayadapter
public class Example extends listactivity{

string[] * * * = new String () {"Male", "female"}//data source

Arrayadapter adapter;//array adapter, with generic type

Public voidoncreate (Bundle savedinstancestate) {

Super.oncreate (Savedinstancestat);

When initializing the adapter, load the data source into the adapter,//this. Android.r.layout.simple_list_item_1 this sentence

Consciousness is to put the data source in a system-defined style into the adapter.


Adapter=newarrayadapter (this. android.r.layout.simple_list_item_1,***);

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, filled with simpleadapter

ListView list = (ListView) Findviewbyid (R.id.mylistview);

Generate dynamic arrays, and reprint data

Arraylist

for (int i=0;i<30;i++)

{

Hashmap<string, string= "" >map = new hashmap<string, string= "" > ();

Map.put ("Itemtitle", "This is Title ...");

Map.put ("Itemtext", "This is Text ...");

Mylist.add (map);

}

Build adapter, Array = = = "ListItem

Simpleadapter mschedule = new Simpleadapter (this,//no explanation mylist,//data source R.layout.my_listitem,//listitem XML implementation//dynamic array with ListItem the corresponding subkey


New string[]{"Itemtitle", "Itemtext"},//listitem XML file inside of two TextView ID new int[] {r.id.itemtitle,r.id.itemtext});

Add and Show

List.setadapter (Mschedule);

}

Three, should say two examples are not difficult, are some we often see the usage, then for simplecursoradapter and how to use it, Simplecursoradapter is generally used mainly for the database, Its data sources are generally database queries get the cursor we 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: one containing the columns used

The name of the other (same size) array contains the resource ID in the view to display the number of the corresponding column

Data values.

Mylistview.setadapter (Myadapter);

We bind a cursor to the ListView and use a custom layout display to display each item.

Four, let's define our own adapter.

Why define your own adapter, the reason is that when we want to use some other means of presentation, or we need to present the way, it is necessary to DIY.

First we define a class to inherit from Baseadapter, and then let it implement a few of the methods described in it. Then this custom adapter is good.

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 it here, you can customize the layout, and I won't say much here.

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, the adapter is ready for use.

Commonly used adapters in Android and define their own adapters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.