Detailed explanation and example code of Android listview and adapter

A listview usually has two responsibilities.

(1) Populates the layout with data. (2) Handle user selection, click and other operations.

The first point is easy to understand. Listview implements this function. The second point is not difficult to achieve. In the later study, readers will find that it is very simple.

The creation of a listview requires three elements.

(1) The view of each column in the listview. (2) Fill in the data or pictures of the view. (3) Adapter for connecting data to listview.

That is, to use listview, you must first understand what an adapter is. The adapter is a bridge connecting data and AdapterView (listview is a typical AdapterView, and others will be learned later). It can effectively separate the setting of data and AdapterView, making the binding between AdapterView and data easier and easier to modify

Android provides many adapters. Table 4-5 lists several commonly used adapters.

Table 4-5 common adapters

In fact, there are many adapters. It should be noted that various adapters only have different conversion methods and capabilities. Next, bind the data for the listview by using different adapters (the simplecursoradapter will not be discussed for the time being, but will be introduced later when talking about SQLite).

4.12. 1 listview uses arrayadapter

Simple listview data binding can be realized with arrayadapter. By default, the arrayadapter binds the toString value of each object to the textview control predefined in the layout. The use of arrayadapter is very simple.

example:

Project catalogue: ex_ 04_ twelve

Add a listview control to the layout file.

Then initialize in the activity.

Analyze the steps used.

(1) Define an array to store the contents of item in listview. (2) Create an object of arrayadapter by implementing the constructor of arrayadapter. (3) Bind arrayadapter through the setadapter() method of listview.

In the second step, it is necessary to say that arrayadapter has multiple constructors, and the most commonly used one is implemented in the example. The first parameter is the context, and the second parameter is a layout resource ID containing textview, which is used to fill each row of the listview. The third parameter is the contents of the listview. The second parameter can customize a layout, but the layout must have a textview control. Generally, we use the resources provided by Android. In addition to those used in the example, there are the following commonly used resources, which can be implemented with RadioButton and Check@R_377_2419 @Listview.

(1) By specifying Android R.layout. simple_ list_ item_ The checked resource implements a listview with a selection box. You need to use setchoicemode() method to set whether the selection is multi-choice or single choice, otherwise the selection effect will not be realized, and the operation effect is shown in Figure 4-30. The implementation code is as follows:

lv. setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,strs)); lv. setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

(2) By specifying Android R.layout. simple_ list_ item_ multiple_ Choice is the resource implementation band Check@R_377_2419 @Listview. Similarly, you need to use the setchoicemode () method to set single or multiple choices. The operation effect is shown in Figure 4-31. The implementation code is as follows:

lv. setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,strs)); lv. setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

(3) By specifying Android R.layout. simple_ list_ item_ single_ Choice this resource implements a listview with a RadioButton. It should be noted here that radio is not specified here. Whether to select multiple or single items should be specified through the setchoicemode() method. The operation effect is shown in figure 4-32. The implementation code is as follows:

▲ figure 4-31 belt Check@R_377_2419 @Listview for

▲ figure 4-32 listview with RadioButton

As mentioned earlier, the listview is responsible for not only filling in data, but also handling user operations. The following code can bind a click listener to the listview, and the number of rows clicked will be displayed in the title bar after clicking.

4.12. 2 listview uses simpleadapter

Many times, you need to display something other than text, such as pictures, etc. in the list. You can use simpleadapter at this time. The use of simpleadapter is also very simple, and its functions are also very powerful. You can customize the contents of items in the listview, such as pictures, multiple selection boxes, etc. Take an example to implement a listview with an ImageView and textview for each line. Take a look at the operation effect first, as shown in figure 4-34.

▲ figure 4-34 listview with Icon

First, add a listview control to the layout file. You also need to define the layout of each row in the listview, and use relativelayout to implement a layout with two lines of words and one picture.

item. xml:

After configuration, you can bind data for listview in Java code.

The data using simpleadapter is generally a list composed of HashMap, and each section of the list corresponds to each row of listview. Through the constructor of simpleadapter, map the data of each key of HashMap to the corresponding control in the layout file. This layout file is generally defined according to your own needs. Comb through the steps of using simpleadapter.

(1) Define the layout implemented by each row of listview as needed. (2) Define a list composed of HashMap and store data in it in the form of key value pairs. (3) Construct a simpleadapter object. (4) Bind lsitview to simpleadapter.

4.12. 3 listview uses baseadapter and listview optimization. In the use of listview, sometimes it is necessary to add buttons and other controls to realize separate operations. In other words, the listview is no longer just displaying data, nor is it just this line to handle the user's operations, but the controls inside should get the user's focus. Readers can try simpleadapter to add a button to the listview item. They will find that they can add, but they can't get the focus. The click operation is overwritten by the listview item. At this time, the most convenient way is to use the flexible adapter baseadapter.

▲ figure 4-35 method in baseadapter

To use baseadapter, you must write a class to inherit it. At the same time, baseadapter is an abstract class, and its methods must be implemented to inherit it. The flexibility of baseadapter is that it needs to rewrite many methods. See which methods are available. As shown in Figure 4-35, the methods implemented by speechlistadapter inherited from baseadapter, the most important of which is getview () method. What are the effects of these methods? We analyze the principle of listview to answer for readers.

When the system starts drawing the listview, first call the getcount () method. Get its return value, that is, the length of the listview. Then the system calls the getview () method to draw each row of the listview one by one according to this length. That is, if getcount () returns 1, only one row is displayed. Getitem () and getitemid () are called when the data in the adapter needs to be processed and obtained. So how to use getview? If there are 10000 rows of data, draw 10000 times? This will definitely consume a lot of resources and cause the listview to slide very slowly. What should I do? An example is given to explain how to optimize the display of listview when using baseadapter. In the example, the ImageView in the previous section is replaced with a button, and the click event of the button is handled. The display of the listview is optimized.

The layout file is similar to the previous example. Readers can view it in the project directory of the CD. Only the activity class is given here.

The operation effect is shown in Figure 4-36. It should also be noted that the button will grab the focus of the listview. You need to set the button to have no focus. The setting is very simple. Just add a line of Android: focusable = "false" code under the button tag of XML. Observe the output information after clicking logcat, as shown in figure 4-37.

▲ figure 4-36 listvie using baseadapter

▲ figure 4-37 output obtained by clicking listview entry and button

The getview () method in the code is not easy to understand. In fact, you can directly import the layout and set the content displayed by the control without the so-called convertview and viewholder. However, this means that you need to draw as many rows of listview as there are rows of data, which is obviously undesirable. An optimization method is adopted here. In the code, a line of log output convertview is added to the getview () method. Scroll listview to output information, as shown in figure 4-38.

As can be seen from figure 4-38, when the activity is started to render the listview on the first screen, the convertview is zero. When the user scrolls down the listview, the upper entry becomes invisible and a new entry appears below. At this time, the convertview is no longer empty, but creates a series of convertview values. When you scroll down again, you find that the container in line 11 is used to hold line 22 and the container in line 12 is used to hold line 23. In other words, convertview is equivalent to a cache, starting at 0. When an entry becomes invisible, it caches its data, and the subsequent entries only need to update the data, which greatly saves the cost of system data.

You can also continue to optimize. Although the view that has been drawn is reused, to obtain the control, you need to obtain it through the findviewbyid method in the control container. If the container is very complex, it will obviously increase the overhead of system resources. In the above example, the concept of tag is introduced. It may not be the best way, but it does make listview smoother. In the code, when the convertview is empty, bind a viewholder object storing the control for each view with the settag () method. When the convertview is not empty and the created view is reused, use the gettag () method to obtain the bound viewholder object, so as to avoid the layer by layer query of findviewbyid on the control, but quickly locate the control.

▲ figure 4-38 value of convertview output by scrolling listview

To sum up, this section introduces how to bind listview data with baseadapter. Because baseadapter is very flexible, it is also more troublesome to use than other controls. At the same time, the optimization of listview is also worth studying. A smooth listview will bring a better user experience.

▲ figure 4-38 value of convertview output by scrolling listview

To sum up, this section introduces how to bind listview data with baseadapter. Because baseadapter is very flexible, it is also more troublesome to use than other controls. At the same time, the optimization of listview is also worth studying. A smooth listview will bring a better user experience.

The above is the data sorting of Android listview and adapter. Continue to supplement relevant data in the future. Thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>