Summary of the usage of Android adapter

1. Concept

The adapter is an adapter interface connecting the back-end data and the front-end display. It is an important link between the data and the UI (view). Adapters are required in common views (listview, GridView) and other places. As shown in the figure below, the relationship among data, adapter and view is intuitively expressed:

2. Application cases

1)ArrayAdapter

The display of the list requires three elements:

a. Listveiw is used to display the view of the list.

b. The adapter is used to map the data to the mediation on the listview.

c. The data is a specific string, picture, or basic component to be mapped.

Case 1

Case 2

The above code uses arrayadapter (context context, int textviewresourceid, list < T > objects) to assemble data. To assemble these data, an adapter connecting listview view object and array data is required to adapt them. The construction of arrayadapter requires three parameters, which are this and layout file (note that the layout file here describes the layout of each line of the list. Android. R.layout. Simple_list_item_1 is the layout file defined by the system. Only one line of text is displayed, and the data source (a list set) is displayed. At the same time, setadapter() is used to complete the final adaptation. The effect diagram is as follows:

2)SimpleAdapter

Simpleadapter has the best scalability. It can define various layouts, including ImageView (picture) and button (button), Check@R_84_2419 @(check box) and so on. The following code directly inherits listactivity. Listactivity is not much different from ordinary activities. The difference is that many optimizations have been made to display listview, just in terms of display.

Case 1

simple. xml

Case 2

The following program is to implement a class table with pictures. First, you need to define an XML to display the contents of each column, vlist xml

The data using simpleadapter is generally a list composed of HashMap, and each section of the list corresponds to each row of listview. Each key value data of the HashMap is mapped to the component with the corresponding ID in the layout file. Because there is no corresponding layout file available in the system, we can define a layout vlist xml。 The following adaptations are made: new, a simpleadapter parameter, once: this, layout file (vlist. XML), HashMap's title and info, img. Component ID, title, info, IMG of the layout file. Each component of the layout file is mapped to each element of the HashMap to complete the adaptation.

The operation effect is as follows:

3)SimpleCursorAdapter

The simplecursoradapter can only be used when the database is used as the data source. Here's a special note: don't forget to use the Android manifest Add permissions to XML files

The effects are as follows:

4)BaseAdapter

Sometimes, the list is not only used for display, we can also add buttons on it. To add a button, first write an XML file with buttons, and then you will naturally think of using the above method to define an adapter, and then map the data to the layout file. But this is not the case, because the button cannot be mapped. Even if you successfully display the button in the layout file, you cannot add the response of the button. At this time, we need to study how the listview is realistic, and we must rewrite a class to inherit baseadapter. The following example will show a button and a picture, two lines of words. If you click the button, the line of the button will be deleted. And tell you exactly how listview works.

vlist2. xml

The above code will be explained in detail below. When listview starts drawing, the system first calls the getcount() function to get the length of listview according to its return value (which is why the list length is specially marked in the first figure at the beginning), and then calls getview() to draw each line one by one according to this length. If your getcount () return value is 0, the list will not display the same return 1, but only one line.

When the system displays the list, first instantiate an adapter (here the custom adapter will be instantiated). When the adaptation is completed manually, the data must be mapped manually, which requires overriding the getview () method. The system will call this method when drawing each row of the list. Getview () has three parameters, position indicates which line will be displayed, and covertview is the layout from the layout file. We use the layoutinflator method to define vlist2 The XML file is extracted into a view instance for display. Then instantiate each component in the XML file (simple findviewbyid () method). In this way, the data can be mapped to each component. However, in order to respond to the click event, the button needs to add a click listener to it, so that the click event can be captured. So far, a custom listview has been completed. Now let's go back and review the process again. The system is going to draw the listview. First, it obtains the length of the list to be drawn, and then starts to draw the first row. How to draw it? Call the getview() function. In this function, first obtain a view (actually a ViewGroup), and then instance and set each component to display it. All right, I've finished drawing this line. Then draw the next line until it is finished. During the actual operation, it will be found that each line of listview has no focus. This is because the button grabs the focus of listview. It is OK as long as the button is set to no focus in the layout file.

The effects are as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>