Android Learning Adapter (data adapter)

Source: Internet
Author: User

1. Definition

data Adapters are Adapterview views such as ListView-List View control, Gallery-thumbnail browser Control, GridView-Grid control, Spinner-drop-down list control, AUTOCOMPLETETEXTVI EW-Auto hint text box, Expandablelistview-a list control that supports expand/shrink functions, etc.) is a bridge between data and is used to process data and bind data to Adapterview.

2. Role

data Adapters are bridges that connect data sources and view interfaces.

3. Classification

include Arrayadapter (array adapter) and Simpleadapter (simple adapter). which

    1. Arrayadapter is used to bind the relative single data, the data source is mainly set or array;
    2. Simpleadapter is used for binding the complex data, the data source can only be a generic collection;

4. Implement

    1. New Adapter;
    2. Add data source to adapter;
    3. View load adapter;

5. Case studies

(1) Specific use of the class:

    1. Arrayadapter (context, the layout file that corresponds to each list loaded by the current ListView, data Source)
    2. Simpleadapter (context,data,resource,from,to) wherein,context: Contexts, data sources (list<? extends Map<string,? >> data) A map consists of a list of all the maps that correspond to a row in the ListView list each key in a map (key-value pair) must contain all the keys specified in the From, resource: The layout file ID of the list item, The key name in the From:map (string[] format), to: The ID in the bound Data view, and the from in the corresponding relationship (int[] format);

(2) Code example:

    • Arrayadapter
1 ListView ListView;2 Arrayadapter Arrayadapter;3 4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_violet);8ListView =(ListView) Findviewbyid (R.id.listview);9String[] data={"Apple", "banana", "peach", "watermelon"};TenArrayadapter=NewArrayadapter ( This, R.layout.Abc_simple_dropdown_hint, data); //context, the layout file corresponding to each list loaded by the current ListView, data Source)
One Listview.setadapter (Arrayadapter);
A}
    • Simpleadapter
1 ListView ListView;2 Simpleadapter Simpleadapter;3 4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_violet);8ListView =(ListView) Findviewbyid (R.id.listview);9list<map<string, object>> data =NewArraylist<map<string, object>>();Tenstring[] persons = {"Miss Zhang", "Teacher Chen", "Miss Li", "Miss Huang"}; OneString[] Phonenums = {"18706218201", "18706214521", "13908761231", "18512390812"}; A          for(inti = 0; i < persons.length; i++) { -hashmap<string, object> map =NewHashmap<string, object>(); -Map.put ("username", Persons[i]); theMap.put ("Phonenum", Phonenums[i]); -             //Insert Picture -Map.put ("Photo", r.drawable.cc); - data.add (map); + }
- //(context,data,resource,from,to) +Simpleadapter =NewSimpleadapter ( This, data, R.layout.list_item,Newstring[]{"username", "photo", "Phonenum"},New int[]{r.id.username,r.id.photo, r.id.phonenum}];
AListview.setadapter (simpleadapter); at}

Android Learning Adapter (data 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.