Adapter Control-adapter

Source: Internet
Author: User

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

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.