Reputation: 223
in my listview i am populating datas which come from a background thread and i also called notifydatasetchanged(),still it gives exception.does anybody have solution to this.
Upvotes: 0
Views: 157
Reputation: 136
If you can get activity, you can use
getActivity().runOnUiThread(new Runnable() {
public void run(){
// update your dataset and call notifydatasetchanged() here
}
});
If not,
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
public void run(){
// update your dataset and call notifydatasetchanged() here
}
});
Upvotes: 0
Reputation:
make another arraylist and do all processing like adding and deleting data's over it and after all operation just copy all data of that arraylist to your arraylist which you have given to your array adapter,so the aa will not give you the illegalstateexception.
Upvotes: 1
Reputation: 23432
I think the other two answerers [there must be a better word for this] have sort of hit it on the head, but hopefully I can be more clear:
notifyDatasetChanged
from the UI thread: your are not, which is why you are seeing an exceptionI'd recommend using AsyncTask, just because it makes dealing with threads a bit easier if you're just beginning:
doInBackground
and thenonPostExecute
method call adapter.notifyDatasetChanged
(the onPostExecute
and onPreExecute
methods of AsyncTask
run on the UI thread automatically to make your life easy).Upvotes: 0
Reputation: 6081
If you want to ensure that the dataset changed notification is done only after completion of thread, while running thread use handler and in handler.postdelayed execute the notifydatasetchanged() command.
Upvotes: 0
Reputation: 6023
illegalstate exception means am thinking that thread you are using is not correctly handling.. can you post your code..so can find where the exact exception happens
Upvotes: 0