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)