What is the role of adapter? In fact, it is a bridge between the view interface and the data. We can be seen as an understanding of interface data binding, and the data it manipulates are generally more complex data, such as arrays, lists, databases, collections, etc.
The commonly used adapters are:
(1) arrayadapter; array as the data source, filled with arrayadapter
(2) Simpleadapter; list as a data source, populated with Simpleadapter
(3) Simplecursoradapter; Data sources are usually the cursor that the database query gets
(4) custom adapter; Why do you define your own adapter? The reason is, when we want to use some other way 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.
Let's take a look at the following example to understand:
One. Arrayadapter
1.xml Code:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7 <ListView8 Android:id= "@+id/myarraylist"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" /> One A </LinearLayout>
2.java Code:
1 PackageCom.example.arrayadapter;2 3 Importjava.util.ArrayList;4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.widget.ArrayAdapter;7 ImportAndroid.widget.ListView;8 9 Public classMainactivityextendsActivity {Ten PrivateListView Mlistview; One PrivateArraylist<string> marraylist =NULL; A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Mlistview =(ListView) Findviewbyid (r.id.myarraylist); -Mlistview.setadapter (NewArrayadapter<string> ( This, + Android. R.layout.simple_expandable_list_item_1, GetData ())); - } + A PrivateArraylist<string>GetData () { atMarraylist =NewArraylist<string>(); -Marraylist.add ("Item1"); -Marraylist.add ("Item2"); -Marraylist.add ("Item3"); -Marraylist.add ("Item4"); -Marraylist.add ("ITEM5"); inMarraylist.add ("Item6"); -Marraylist.add ("Item7"); toMarraylist.add ("ITEM8"); +Marraylist.add ("Item9"); -Marraylist.add ("ITEM10"); the returnmarraylist; * } $}
Two.
Android Learning Adapter Adapter