Adapter optimization in the ListView of Android Development

Source: Internet
Author: User

The ListView is the most commonly used control for Android development, and the adapter adapter is the data that will be displayed in the view and added to the ListView display
When implementing the ListView, we need to define the adapters such as Baseadapter, Arrayadapter, CursorAdapter, Simpleadapter, and so on, and rewrite them for a few four methods:
1 public int getcount ();
2 public Object getItem (int position)
3 Public long Getitemid (int position)
4 public View GetView (int position, View Convertview, ViewGroup parent)


We need to define a view to display each piece of information and finally add it to the ListView.

For example, we define a view file: List_item_callsms.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "> <textview androi d:layout_margintop= "3dip" android:layout_marginleft= "10dip" android:id= "@+id/tv_black_number" Android : layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "blacklist number" Android:textco Lor= "#88000000" android:textsize= "20sp"/> <textview android:layout_margintop= "3dip" Android : layout_marginleft= "10dip" android:id= "@+id/tv_black_mode" android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:layout_below= "@id/tv_black_number" android:text= "interception mode" Android : textcolor= "#88000000" android:textsize= "15SP"/></relativelayout>


When we implement adapter, let's say we do this:

Private list<blacknumberinfo> Infos;private class Callsmssafeadapter extends Baseadapter {@Overridepublic int GetCount () {//returns how many return Infos.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;} How many entries this method will be called @overridepublic view getView (int position, view Convertview, ViewGroup parent) {//To convert a layout file into a VI EW objects are called very memory-intensive every time view view = View.inflate (Getapplicationcontext (), r.layout.list_item_callsms, NULL); TextView Tv_black_number = (TextView) View.findviewbyid (R.id.tv_black_number); TextView Tv_black_mode = (TextView) View.findviewbyid (R.id.tv_black_mode); Tv_black_number.settext (Infos.get ( Position). GetNumber ()); String mode = "intercept All", if (infos.get (position). GetMode (). Equals ("1")) {mode = "phone intercept";} else if (Infos.get (position). GetMode (). Equals ("2")) {mode = "sms Blocker";} Tv_black_mode.settext (mode); return view;}}

There is a problem, when the data set to display more than 100 times (infos), when we quickly slide the ListView, the interface will show that the program is unresponsive, and logcat in the non-stop print DVM virtual machine GC garbage collection data (actually each defined View object) ; This does not seriously affect the performance of the program and the user experience;


So for the above, the adapter used by the ListView can be optimized in two ways:

1. Reduce the number of creation times for view objects in memory.

The view in the ListView is created in the first visible list (Converview is null), and when the slide is loaded again, the view created between the Converview will be reused and then the list of visible view objects is looped.

ConvertviewThe old view to reuse, if possible. Note:you should check the This view is non-null and of a appropriate type before using. If It isn't possible to convert this view to display of the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so it this view are always the right type (see getViewTypeCount()and getItemViewType(int)).

2. Reduce the number of lookups in the view for the corresponding control.

Each time you look up a control in a view is quite time-consuming, equivalent to iterating through the XML tree of the view, so we only need to iterate through the first one and then reuse it directly.

So we can create a view cache and then use the view's tag to hold the address of the control in memory. So we create up to a viewlist visible length view to find the ID of the control in the view.

This can greatly improve the performance of the program.


The optimized code is as follows:

Private class Callsmssafeadapter extends Baseadapter {@Overridepublic int getcount () {//returns how many bars return infos.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;} How many entries this method will be called How many times @overridepublic View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;//to convert a layout file into a View object each time it is called very memory-intensive//1. Reduce the number of in-memory View object creation if (Convertview = = null) {//converview is empty when creating Convertview = View.inflate (Getapplicationcontext (), R.layout.list_item_callsms, null); holder = new Viewholder ();//2. Reduce the number of control queries in view Holder.tv_number = (TextView) Convertview.findviewbyid (r.id.tv_black_number); Holder.tv_mode = ( TextView) Convertview.findviewbyid (R.id.tv_black_mode);//Keep viewholder in the tag of view Convertview.settag (holder);} else {//Get Viewholderholder = (viewholder) Convertview.gettag () in the tag of the view;} Holder.tv_number.setText (Infos.get (position). GetNumber ());String mode = "intercept All", if (infos.get (position). GetMode (). Equals ("1")) {mode = "phone intercept";} else if (Infos.get (position). GetMode (). Equals ("2")) {mode = "sms Blocker";} Holder.tv_mode.setText (mode); return convertview;}} /** * Record the memory address of a control in view equivalent to a notepad * @author Zty * */static class Viewholder{textview Tv_number; TextView Tv_mode;}





Adapter optimization in the ListView of Android Development

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.