I bvelieve the problem is your doInBackground clearing the data, while
it's still being accessed by the list view (possibly scrolling).
An easy way to fix this is to ping-pong two data sets. Each would be
owned by the respective thread (UI vs. download). The async task would
work with one, while not touching the other, leaving it immutable for
the UI thread to use. Once the download is complete, switch the data sets.
And actually, with Java, you can just create a new data set every time
through the loop in the async task, then send it to the UI thread to be
displayed, until next time. Make sure your UI code doesn't keep any
references to the previous data set, so it can be garbage collected.
-- Kostya
29.07.2011 19:41, GBF пишет:
I'm working on an application that primarily consists of a list view.
It's backed up by my own custom array adapter whose size changes every
5 seconds. If I scroll through the list view as the array adapter
changes from a greater size to a lesser size, I get an out of bounds
exception. It makes sense to me why this occurs (since I'm scrolling
at a position beyond the new array size), but I was wondering if there
was a good way to debug it. I can't seem to come to a clear
conclusion, and I was wondering if I could get some help.
I update the adapter using the following asyncTask...
public class myTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
while(isRunning){
myData.clear();
getData();
publishProgress();
SystemClock.sleep(5000);
}
return null;
}
protected void onProgressUpdate(Void...progress){
listAdapter.notifyDataSetChanged();
}
}
myData is the ArrayList that supports listAdapter and getData() is the
function that populates myData with the relevant info that will
eventually be displayed in my list view.
Is there a good way to tackle this problem? For instance, should I try
to momentarily disable scrolling when going from larger to smaller, or
should I try to take care of it from the adapter's perspective?
Regards
--
Kostya Vasilyev
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en