Android Load Data listview optimized memory implementation

Source: Internet
Author: User

Problem analysis

In the mobile project development, based on the reason of loading large data, often memory overflow phenomenon, then reduce the memory footprint, in real development is really important, the following introduction of the following ListView load data Large-scale solution idea:

We should have encountered this situation, for the load of the ListView, we slowly sliding the data, the system loading data is not a problem, but when the sliding data, there will be a memory overflow problem (this does not consider the page load method). Summary this is because the phone screen display reason, each load data is limited, slowly sliding data, while loading data, the system can have time to reclaim the unused memory, will not expose the problem, but, speed up the slide, memory recovery GC Too late, slowly memory is full. Knowing the reason, increasing the memory, slowing down the sliding speed is obviously not a good solution, and implementing memory reuse is the most important.

Solution Solutions

We know that the data is finally displayed on the phone screen, is implemented through the adapter adapter, in its default implementation method GetView () method, we implement the following code:

How many entries are displayed, how many times will this method be called @overridepublic view GetView (final int position, view Convertview, ViewGroup parent) {view view; Viewholder HOLDER;//1. Reduce the number of view object creation in memory (optimize listview, avoid memory overflow) if (convertview==null) {log.i (TAG, "Create New View object:" +position );//Convert a layout file into a View object. View = View.inflate (Getapplicationcontext (), r.layout.list_item_callsms, NULL)//2. Reduce the number of child queries the address of an in-memory object. Holder = new Viewholder (); holder.tv_number = (TextView) View.findviewbyid (r.id.tv_black_number); Holder.tv_mode = ( TextView) View.findviewbyid (r.id.tv_block_mode); holder.iv_delete = (ImageView) View.findviewbyid (r.id.iv_delete); /When the child is born, find their references, store them in Notepad, and place them in the Father's Pocket View.settag (holder);} ELSE{LOG.I (TAG, "The kitchen has a historical view object, re-uses the historical cached View object:" +position); view = Convertview;holder = (viewholder) view.gettag ();// Performance optimization by encapsulating objects 5%, very small, here does not give the holder class, as memory address multiplexing effect significantly}holder.tv_number.settext (Infos.get (position). GetNumber ()); String mode = infos.get (position). GetMode ();//Delete event response, save it here later with Holder.iv_delete.setOnClickListener (new Onclicklistener () {@Overridepublic void OnclIck (View v) {alertdialog.builder builder = new Builder (callsmssafeactivity.this); Builder.settitle ("warning"); Builder.setmessage ("Are you sure you want to delete this record?") ");//dialoginterface interface class method Builder.setpositivebutton (" OK ", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//delete the contents of the Database Dao.delete (infos.get (position). GetNumber ());//update interface. Infos.remove (position);//Notify ListView Data Adapter update adapter.notifydatasetchanged ();}); Builder.setnegativebutton ("Cancel", null); Builder.show ();}}); return view;}
The implementation method looks at the code analysis. By the way, there is a delete operation code in the code that focuses on how the data is updated into the ListView after the addition of the Delete, which is the adapter.notifydatasetchanged () method.


Android Load Data listview optimized memory implementation

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.