hitansu jena
hitansu jena

Reputation: 223

Illegalstate exception in listview

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

Answers (5)

yst
yst

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

user5461818
user5461818

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

Joseph Earl
Joseph Earl

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:

  • You must call notifyDatasetChanged from the UI thread: your are not, which is why you are seeing an exception
  • So what you need to do is populate your cursor in a background thread
  • Then update the UI from a handler once that is complete

I'd recommend using AsyncTask, just because it makes dealing with threads a bit easier if you're just beginning:

  • Populate your cursor in doInBackground and then
  • In your onPostExecute method call adapter.notifyDatasetChanged (the onPostExecute and onPreExecute methods of AsyncTask run on the UI thread automatically to make your life easy).

Upvotes: 0

Vicky Kapadia
Vicky Kapadia

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

Uday
Uday

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

Related Questions