how to use lazy load or async task in List View
I am making a list of view in my application that shows all installed applications in the users device. It shows the name and the icon. The list view takes very long to load and the UI is unresponsive while its loading. I have seen the resources that have lazy load and async task tutorials but they all seem to fetch images from the internet. I need to lazy load 开发者_运维技巧images that are in the system (which are the application icons). I dont know how to do this with async task either. Can someone please help me lazy load or asynctask application icons. This is a very essential part of my application and i would deeply appreciate it. Thanks.
So I usually don't offer advice if there isn't any clear attempt/code to show for, but I remember when I was first confronted with AsyncTasks and threading in general and how it was a bit confusing at first, so I'll get you started on the right track.
So an AsyncTask basically runs a process that may take a while (such as loading information from a server or, in your case, fetching files). It has a couple methods that are detailed here, but I believe, for your scenario, you will simply need to use the doInBackground
and onPostExecute
methods. What you'll probably be doing in each of those is getting the actual images and data for your ListView in doInBackground
and then updating the ListView to display that data in onPostExecute
. Consider the examples outlined here. Essentially, the doInBackground
method will send some data (in this case your files and other stuff - usually in a form of an array or List or something if there's lots of data being sent) to the onPostExecute
method which will handle the data from there.
What's happening is that you're overburdening your main UI thread and the program will wait for the fetching and loading of the images intermittently with your UI. AsyncTask
takes care of this for you by throwing all the work to separate worker threads. Problems usually arise when overburdening the UI thread, such as your program closing unexpectedly because Android notices that the main application thread is using too many resources (no actual bugs in your code).
As for the Lazy Load of the image data, I'm not completely sure how it works (never had to use it), but this seems really helpful.
Hope that gave you some direction.
精彩评论