In one Activity, I am doing this:
This is done every minute, over and over.
My problem is: It seems to be a little slow? Sometimes when I swipe down the list, it's slow? And when I click, the response is not immediate...and may require a little hold.
Do you think I can fix this with multiple "threads" or something? Please advise. Thanks.
Edit: When it's loading the names and user-icons into the list...it's practically unusable. The list freezes...and you can't swipe up or down. And when it's not loading ...the list swipes very slowly.
For actions like downloading stuff "in" an Activity it is always useful to have background services to do the work for the Activity that then stays responsive! If you try to do stuff like that within the Activity itself, you definitely get in trouble with responsiveness, I can assure you! So: my answer is - yes. Implement a background service for that!
Threading and caching are probably good ideas. If your list is only ten people (10 times 2kB of memory, 20 kB) then consider using a cached list for the UI and update it after each download. Check the AsyncTask class that was introduced with Android 1.5 for this. A simple code sample could look something like this:
mCachedList;
private void onUpdate(Location location) {
DownloadTask refresh = new DownloadTask(location);
refresh.execute();
}
private class DownloadTask extends AsyncTask<Location, void, ArrayList<ListItem> {
protected ArrayList<ListItem> doInBackground(Location... params) {
final ArrayList<ListItem> refreshlist = new ArrayList<ListItem>;
Location location = params[0];
sendToCloud(location);
while(!done) {
ListItem next = downLoadPerson();
refreshlist.add(next);
}
return refreshlist;
}
protected void onPostExecute(ArrayList<ListItem> refreshlist) {
mCachedList = refreshlist;
}
}
Use the cached list for the UI until the background task finishes and it is refreshed. There are more advanced things you could do to save more memory like reusing the refreshlist etc but I hope the above provides some info on how to do this in the background. Also remember to reuse the convertview in your list to avoid a lot of garbage collection on each item when you are scrolling in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With