Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update ListView in the main thread from another thread

Tags:

android

I have a separate thread running to get data from the internet. After that, I would like to update the ListView in the main thread by calling adapter.notifyDataSetChanged(). But it does not work. Any workaround for that? Thanks.

like image 524
Wilson Avatar asked May 27 '10 09:05

Wilson


2 Answers

Assume yourActivity is the activity that your widget has been placed into it, yourView is The widget and adapter is The widget's adapter:

yourActivity.runOnUiThread(new Runnable() {
 public void run() {    
         try{
                adapter.notifyDataSetChanged();
         }
         catch{}
 }
}
like image 102
Mostafa Lavaei Avatar answered Sep 23 '22 06:09

Mostafa Lavaei


Or, post a message to the listview's message queue (which would execute on the UI thread):

list.post(new Runnable() {                  
    @Override
    public void run() {
       adapter.notifyDataSetChanged();

    }
}); 
like image 21
Jay Sidri Avatar answered Sep 22 '22 06:09

Jay Sidri