Android--adapter

Source: Internet
Author: User

adapter--itself is just an interface. Adapter is a bridging class that binds data to the UI interface. Adapter is responsible for creating a child view that displays each item and providing access to the underlying data.

Data adapters function: Populate complex data (arrays, lists, databases, collections, etc.) on the specified view interface.

Data adapters are bridges that connect data sources and view interfaces

Introduce the three most common implementation classes:

1. Arrayadapter: Easy to use, typically used to wrap multiple values of an array or list collection into multiple list items. Each list item can only be TextView.

Implementation process: New adapter--Add data source to adapter--View Load Adapter

// define an array    String[] arr1 = {"AA", "BB", "CC"};         // wrap the array as Arrayadapter        New Arrayadapter<string> (this, r.layout.array_item,arr1);         // set Adapter        for the ListView List.setadapter (Adapter1); // list is a ListView component

The following three parameters must be specified when creating a arrayadapter:

Context: Represents an interface for accessing an entire Android application

Textviewresourceid: A resource ID that represents a textview that TextView component will act as the list item component of the Arrayadapter. (Control each list item component)

Array or list: The array or list will be responsible for providing data for the number of list items. (Provide data for list items)

2. Simpleadapter: Powerful, can be used to wrap multiple objects of the list collection into multiple list items

Implementation process: New adapter--Add data source to adapter--View Load Adapter

A small example: A list item contains: The leftmost icon, and then the same row above the title, here are some explanatory text.

Three arrays are defined first, each of the three categories is stored.

    //Create a list collection with the elements of the list set as mapList<Map<String,Object>> ListItems =NewArraylist<map<string, object>>();  for(intI =0;i<names.length;i++) {Map<String,Object> ListItem =NewHashmap<string,object>(); Listitem.put ("Header", Imageids[i]); Listitem.put ("Name", Names[i]); Listitem.put ("Desc", Descs[i]);    Listitems.add (ListItem); }    //Create a SimpleadapterSimpleadapter Simpleadapter =NewSimpleadapter ( This, Listitems,r.layout.simple_item,Newstring[]{"Name", "header", "Desc"},New int[]{r.id.name,r.id.header,r.id.desc}]; ListView List=(ListView) Findviewbyid (r.id.mylist); List.setadapter (simpleadapter);

Five key parameters explained:

Context: Contexts

Data: source. is a list<? Extends a collection object of type map<string,?>> that generates a list item for each Map<string,?> object in the collection

Resource: The layout file ID of the list item

Key names in the From:map

To: The ID in the bound Data view, corresponding to the From

3, Baseadapter: Usually used to be extended. The extended baseadapter can be customized to the fullest extent of each list item.

Android--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.