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
- Arrayadapter is used to bind the relative single data, the data source is mainly set or array;
- Simpleadapter is used for binding the complex data, the data source can only be a generic collection;
4. Implement
- New Adapter;
- Add data source to adapter;
- View load adapter;
5. Case studies
(1) Specific use of the class:
- Arrayadapter (context, the layout file that corresponds to each list loaded by the current ListView, data Source)
- 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:
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}
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)