开发者

listing a listview is taking too much time and memory in android

i 3 classes, one which contains the list view and is the acitivity. second class is a adapter that i have built to populate the listview. third class which interacts with the database and gives info to the second class.

the functioning of all the three classes when i do the following:

listview.setAdapter(new CustomAdapter())

it is taking a whole of time because there are many records to display. i want to use multithreading over here. how do i go about it? i know of AsyncTask. would it work if i kept the above mentioned statement in the doInBackground() method, but how will i know when the list is populated?

SOLUTION:

Got a solution. i am passing the progressdialog object from the mainactivity class into the custom adapter. over there in the getView method i am checking whether the int arg0 (the variable giving the position in the dataset) has reached the end. upon this i dismiss the progressdialog that i have passed开发者_JS百科.


The AsyncaTask class contains methods like onProgressUpdate and publishProgress. So whenever you get some data you could call publishProgress to indicate that some data has been received.

Then in onProgressUpdate, you could show the data. Thus, the user does not have to see a Progress bar for too long and he can view the items as they appear.


The list control is smart enough to create only needed number of views which is equal to the max number of visible view + 2 extra. When you scroll down it reuses the views. I guess that the time consuming operation is in creating a single view. Are you loading something from remote address ?


You could also just show a certain amount of items, and add more items when the user scrolls to the bottom.

Here is a nice example on how to do this;

http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜