Usage and optimization of baseadapter

Source: Internet
Author: User

 

For Android programmers, baseadapter is certainly not unfamiliar. Flexibility and elegance are the biggest feature of baseadapter. Developers can construct a baseadapter and attach it to the layout of multiple controls such as listview or gridview to achieve the layout effect required by the software. At the same time, baseadapter is also the most basic class in the adapter. Others such as simpleadapter and arrayadapter both inherit the baseadapter directly or indirectly. Therefore, if you learn baseadapter well, you will be familiar with the use of the adapter.

This article requires some basic knowledge of Android to better understand, because this article does not introduce the basic usage of baseadapter, if you do not understand baseadapter, you can refer to the document. Here we will mainly introduce the Implementation Details of baseadapter and how to optimize the design of baseadapter to save our valuable mobile phone running resources. For laruence experts, this may be a common sense, or a little reference for developers who have just started as I did before. I also hope that the experts can give me more corrections.

Not to mention, let's take a look at some social software interfaces (found online .....)

This list item uses listview as the layout and carries simpleadapter or baseadapter as the data source for the interface effect. In general, many software interfaces use this method to achieve the desired layout effect. Here I will explain the terms mentioned later. First, this is a list item with various data items in it. The sample, file transfer assistant, and test + time are a data item, it is a view, which contains a total of 8 data items, which are arranged vertically by 8 view views. OK. Let's get started.

For example, we can better understand it. Therefore, we assume that there are a total of 40 data items in this list, and as a limitation of the mobile phone interface, it can only display 8 data items at a time, at the same time, its other 32 data items are hidden. To display these eight data items, you need eight view instances, that is, you need to instantiate the subclass of the eight views to display the content. At this time, the system has eight view instances occupying the memory space. If we slide our fingers down, the data items at the top will be hidden, and the data items at the bottom will naturally be displayed, in this case, do you need to create a new view instance? If you still want to create an instance, there will be nine view instances in the system memory space, but we still need only eight view instances to display them completely on the mobile phone screen, the hidden instance on the top does not exist. In this case, the running resources of mobile phones are wasted.

To solve this problem, we can reuse the view in baseadapter, that is, change the hidden view to the view to be displayed and return it.

In baseadapter, this abstract method is very important. It is to return the view of the object's data items based on the position value. Therefore, we mainly optimize the efficiency of this method.

Public View getview (INT position, view convertview, viewgroup parent) {return NULL ;}

A brief introduction is provided to the parameter of this function. position is the ID index value, that is, the serial number of the corresponding data item of listview, while convertview is the basis for reuse of view views, if it is the data of the first page, its value is null, so we can decide whether to create a new view instance based on the convertview value.

See the following code

1 @ override 2 public view getview (INT position, view convertview, viewgroup parent) {3 if (convertview = NULL) {4 convertview = new textview (context ); // create a new string control 5 (textview) convertview ). settext (position); // specifies the number of data items. 6} 7 else {8 (textview) convertview ). settext (position); // re-modify the number of data items 9} 10 return convertview; 11}

To make it simple, the view here is just a simple string control. In the code, we can see that convertview is judged first, if its value is null, it indicates that the view returned by this method is the view on the first page. Therefore, an instance of the view needs to be created, set the displayed content and return it (the content set here is position ). If its value is not null, the view can reuse other hidden view instances, and the convertview index is the hidden instance, naturally, you do not need to re-create a new view instance and directly reset the value of convertview and return it.

This ensures that there are only a sufficient number of view instances displayed on the interface in the program, and there is no excessive view to occupy system resources, which also improves the efficiency.

 

 

 

 

This is an aspect of baseadapter efficiency optimization. Next, let's talk about another aspect, which is a small skill to improve efficiency. For a baseadapter, the displayed interface is certainly not just one or two string controls (or we will use arrayadapter for simplicity), but is generally to load a layout Control Generated Based on layout, this layout control is relatively complicated. We will obtain its child control and return the result by assigning values. Therefore, we often need to call the findviewbyid () method or reference the child control. Findviewbyid () consumes system resources. frequent calls increase the workload of mobile phones when we quickly slide the interface, so we can make some minor improvements in this area.

Refer to this Code

1 public class personadapter extends baseadapter {2 Private list <person> persons; // bound data 3 private int resource; // bound layout data item interface 4 private layoutinflater Inflater; // layout filling Service 5 6 Public personadapter (context, list <person> persons, int Resource) {7 This. persons = Persons; 8 This. resource = resource; 9 Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); 10} 11 @ override12 public int getcount () {13 return persons. size (); 14} 15 @ override16 public object getitem (INT position) {17 return persons. get (position); 18} 19 @ override20 public long getitemid (INT position) {21 return position; 22} 23 @ override24 public view getview (INT position, view convertview, viewgroup parent) {25 textview nameview = NULL; 26 textview phoneview = NULL; 27 if (convertview = NULL) {28 convertview = Inflater. inflate (resource, null); // generate the View Interface for binding data 29 nameview = (textview) convertview. findviewbyid (R. id. name); 30 phoneview = (textview) convertview. findviewbyid (R. id. phone); 31 32 viewcache cache = new viewcache (); 33 cache. nameview = nameview; 34 cache. phoneview = phoneview; 35 convertview. settag (cache); 36} else {37 viewcache cache = (viewcache) convertview. gettag (); 38 nameview = cache. nameview; 39 phoneview = cache. phoneview; 40} 41 person = persons. get (position); 42 // the following code binds 43 nameview data. settext (person. getname (); 44 phoneview. settext (person. getphone (); 45 46 Return convertview; 47} 48/** 49 * Save the internal class 50 */51 private final class viewcache {52 public textview nameview; 53 public textview phoneview; 54} 55}

Focus on the getview () method. In the code, I wrote a private internal class of viewcache, which is mainly used to save the index value of the Child control required by the view to be reused. A bit of code is explained. If convertview is null, a new view instance is required. Here, the system service Inflater is called to generate a view instance based on the ID value of the resource interface, then, use the findviewbyid method or the sub-control value, save it to the internal viewcache, and put it in the tag, settag (object) it can be saved as a cache in any class because it is an object. Finally, set the content of the view sub-control and return it. If it is not null, We need to reuse the view instance now. So we also need to reuse the sub-control index value of the class member variable of viewcache saved in the tag, instead of calling the findviewbyid () method one by one, you can save a lot of system resources when the interface is complex enough.

This is two tips for baseadapter to improve efficiency. If Daniel has other better skills, he also hopes to share and share the skills to learn.

 

 

 

 

Also, let's talk about a small problem encountered by the use of simpleradapter. Of course, it is also for new users. It is about calling yydatasetchanged () but the data in the list is not changed. In fact, your data source has an error. For example, the following code does not change the data.

List 

Because the reference of the data source has been passed in when the adapter is instantiated, but now it only modifies the reference of the data source to re-point it to another data set, notifydatasetchanged () does not respond, the correct method should be like this.

List 

I wrote this blog randomly. I hope you can correct me if you have any shortcomings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.