adapter--Adapter , I believe you in the development of the use of this class or its subclasses. I also use in many places, so summarize here.
Adapter played a role as a bridge , binding the Adapterview and the data source board together. What is Adapterview? It is actually a view, but a view that needs to be dynamically set up in the code, rather than as other components such as Button,imageview, you can specify attributes and displayed data in an XML file.
Here's a look at the class diagram for adapter and Adapterview:
The subclasses of the Adapterview we generally use are: ListView, GridView, Gallery, spinner, etc., with the most adapter (adapters) Arrayadapter, Simpleadapter, Simplecursoradapter, Baseadapter and so on.
Using adapter generally requires three steps:
1. Set the relevant properties of the Adapterview you are using in the layout file.
2. Use the adapter (adapter and its subclasses) in your code to adapt the Adapterview. You can now display it normally.
3. Add listeners to handle some of the triggering events for Adapterview.
I'll explain these four adapters in more detail below.
First, Arrayadapter
This is the simplest type of adapter that typically wraps an array or list collection into multiple list items. Let's take the ListView as an example.
Look at the layout file first:
<!-- android:divider= "#f00" set the color of the split line android:dividerheight= "2DP" set the height of the split line android: Stackfrombottom= "true" when the list is displayed to the bottom of the list android:scrollbars= "None" hides the scrollbar android:fadescrollbars= "false" when False, the scroll bar always displays, true when hidden-- - <listview android:id= "@+id/array_adapter_lv1" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:background= "#1d953f " android:divider=" #f00 " android:listselector=" #E9967A " android:dividerheight=" 2DP " Android:headerdividersenabled= "false" android:transcriptmode= "Alwaysscroll" > </ListView>
Java Code section:
private void InitListView1 () {listView1 = (ListView) Findviewbyid (R.ID.ARRAY_ADAPTER_LV1); string[] arr1 = new string[] {"Item1", "item2", "Item3"}; arrayadapter<string> adapter1 = new Arrayadapter<> (this,android. R.layout.simple_list_item_1, arr1); Listview1.setadapter (Adapter1);}
There are many constructors for arrayadapter, I use the one with three parameters, and the following explains the meaning of the parameters.
- Context: An interface that represents access to Android apps
- Textviewresourceid: Resource ID, which represents a textview, this textview as a list item component of the Arrayadapter.
- Array or list: Provides data for multiple list items.
The second parameter is more important, you can use the system-provided layout file, or you can define it yourself ( must be textview as the root tag ), as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/textview1 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:textsize=" 12pt " android:textcolor=" #8DB6CD " android:text=" TextView "/>
Then, in the code, you can specify the XML file you defined.
arrayadapter<string> adapter = new Arrayadapter<string> (This,r.layout.array_adapter_item, GetResources ( ). Getstringarray (R.array.array1));
Second, SimpleadapterThird, SimplecursoradapterIv. Baseadapter
Android from zero single row of adapter and its subclasses