[Android] Mobile defender blacklist feature (ListView optimizer)

Source: Internet
Author: User
Tags phoneview

The previous record uses the ListView to display the data, when dragging slowly, there is no problem, but when dragging quickly, the application will report the ANR Error

View error log, see outofmemoryerror, low memory

The ListView will call the GetView () method Every time it displays an entry.

This method converts an XML file into a View object that consumes resources very much, to avoid frequent calls:

In the overridden GetView () method, a View Object is passed in Convertview

When the hand is dragged up the ListView , the entry that hides the item is stored in the Convertview object, which Convertview object can be used as XML converted to target View Object

At this point, creating a new View object will only be created when the first screen is displayed, followed by an entry that is used by the previously cached view Object

The following call to the Findviewbyid () method of the View object is also very memory intensive:

In the android layout, is a typical tree structure, when looking for controls, you need to traverse the entire structure, it may be very time-consuming

Define an inner class Viewholder

Define attributes according to the business

In the GetView () method, get the viewholder Object

Assigns the properties of the viewholder object,Findviewbyid () finds the View Control Object

When the converted View object is created, call the Settag () method of the view object , parameter: Viewholder Object

Use the Gettag () method of the View object to get the viewholder object using the

    classviewholder{ PublicTextView Phoneview;  PublicTextView Modeview; }    Private classMyadapterextendsBaseadapter {@Override Public intGetCount () {//TODO auto-generated Method Stub            returninfos.size (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent)            {View view; Viewholder Holder=NewViewholder (); if(convertview==NULL) {View= View.inflate (callsmssafeactivity. This, R.layout.list_call_sms_safe_item,NULL); Holder.phoneview=(TextView) view. Findviewbyid (R.id.tv_main_phone); Holder.modeview=(TextView) view. Findviewbyid (R.id.tv_block_mode);                View.settag (holder); System.out.println ("Create a new View object" +position); }Else{View=Convertview; Holder=(Viewholder) View.gettag (); System.out.println ("Use Historical View object" +position); } holder.phoneView.setText (Infos.get (position). Get ("Phone")); Switch(Infos.get (position). Get ("mode"))) {             Case"1": Holder.modeView.setText ("Phone Blocker");  Break;  Case"2": Holder.modeView.setText ("SMS Blocker");  Break;  Case"3": Holder.modeView.setText ("Intercept All");  Break; default:                 Break; }                        returnview; }

[Android] Mobile defender blacklist feature (ListView optimizer)

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.