Adapter is used to help fill the middle of the data bridge, the simple point is: a variety of data in the appropriate form of display to the view, provided to the user to see!
Inheritance structure:
Photo from: Manual net
We can see from this picture that there are many subclasses of adapter, the most commonly used in these subclasses include
Arrayadapter: Only one line of text can be displayed, the function is too limited.
Simpleadapter: A adapter that also has good extensibility, you can customize a variety of effects!
Baseadapter: Abstract class, in actual development we will inherit this class and rewrite the related method, the most used one adapter!
Arrayadapter Simple examples of use :
Method one in Mainactivity.java
PackageCom.nianqing.mac.a01_adapter;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {//the data to displayString[] STRs = {"AA", "BB", "CC", "dd", "EE"}; //Create Arrayadapterarrayadapter<string> adapter =NewArrayadapter<string> ( This, Android. R.LAYOUT.SIMPLE_EXPANDABLE_LIST_ITEM_1,STRS); //get the ListView object by calling the Setadapter method to set adapter for the ListView setting adapterListView list_test =(ListView) Findviewbyid (r.id.list_test); List_test.setadapter (adapter); }}
In the Activity_main.xml method, just put a ListView tag and specify the ID as list_test.
Method Two
Create the Arrays.xml file under the Res/values directory:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <String-arrayname= "MyArray"> <Item>Chinese</Item> <Item>Mathematical</Item> <Item>English</Item> <Item>Chinese</Item> <Item>Chinese</Item> <Item>Mathematical</Item> <Item>English</Item> </String-array></Resources>
Modify the listview tag in Activity_main.xml to:
< ListView Android:id = "@+id/list_test" android:layout_width= "Match_parent" android:layout_height= "Match_ Parent " android:entries=" @array/myarray ">
Finally, modify the Mainactivity.java file:
PackageCom.nianqing.mac.a01_adapter;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {Arrayadapter<CharSequence> adapter = Arrayadapter.createfromresource ( This, R.array.myarray,android. R.layout.simple_list_item_multiple_choice); }}
This allows you to display a well-defined list without having to use an array in a Java file.
Using the method above, we see
Arrayadapter.createfromresource (this, r.array.myarray,android. R.layout.simple_list_item_multiple_choice)
This method has two parameters:
The first parameter is this (can be replaced by using mainactivity.this)
The second parameter is r.array.myarray,android. R.layout.simple_list_item_multiple_choice This is a system-provided ListView template that shows the effect of a single line of text
In addition, several additional templates are available:
Simple_list_item_1: A single line of text boxes
Simple_list_item_2: Two text boxes made up of
Simple_list_item_multiple_choice: Option with a check box
Simple_list_item_single_choice: Option with a radio button
Examples of using Simpleadapter
The adapter can implement the ListView of a picture-and-text mix:
First, write a custom layout
List_item.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"> <!--define a ImageView to display the Avatar - <ImageViewAndroid:id= "@+id/imgtou"Android:layout_width= "64DP"Android:layout_height= "64DP"Android:baselinealignbottom= "true"Android:paddingleft= "8DP" /> <!--define a vertical direction of the linearlayout, the QQ is called and the said text box set out - <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical"> <TextViewAndroid:id= "@+id/name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingleft= "8DP"Android:textcolor= "#1D1D1C"android:textsize= "20SP" /> <TextViewAndroid:id= "@+id/says"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingleft= "8px"Android:textcolor= "#B4B4B9"android:textsize= "14SP" /> </LinearLayout> </LinearLayout>
(I write too much trouble, change the code from the manual net)
Mainactivity.java
Public classMainactivityextendsappcompatactivity {//Header Array Privatestring[] names =Newstring[]{"Title1", "Title2", "Title3"}; //Content Array Privatestring[] says =Newstring[]{"Message1", "Message2", "Message3"}; //Image Array Private int[] Imgids =New int[]{r.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //combine a set of data and put it into an item, and then join the listlist<map<string, object>> ListItem =NewArraylist<map<string, object>>(); for(inti = 0; i < names.length; i++) {Map<string, object> ShowItem =NewHashmap<string, object>(); Showitem.put ("Touxiang", Imgids[i]); Showitem.put ("Name", Names[i]); Showitem.put ("says", Says[i]); Listitem.add (ShowItem); } //Create a SimpleadapterSimpleadapter Myadapter =NewSimpleadapter (Getapplicationcontext (), ListItem, R.layout.list_item,Newstring[]{"Touxiang", "name", "says"},New int[]{r.id.imgtou, R.id.name, r.id.says}]; ListView ListView=(ListView) Findviewbyid (r.id.list_test); Listview.setadapter (Myadapter); }}
This will enable simple text-to-picture blending effect. But we see the title, the content, and the pictures are all written dead, unless this is a menu, it is not possible to display the data. Normally, we all get these data through the Web.
And then assemble them and show them, that's what we want.
Do you want to learn? Don't worry, wait till later.
The above two adapters are system-provided adapters, there are many restrictions on the use of these, not often used in development, generally we will customize the adapter to meet some of our own needs.
The next article explains the custom adapter
Adapter (i)