Elbbard
Elbbard

Reputation: 2164

Call adapter.notifyDataSetChanged() from another thread

I want to call adapter.notifyDataSetChanged() from another thread. I read that I should use an AsyncTask and do the adapter.notifyDataSetChanged() in post execute.

I must execute the AsyncTask every 5 seconds only on the current activity (might be parent or child activity) because only one activity can do the asynctask at the same time.

Should I create a TimerTask which executes the AsyncTask every 5 seconds, stop it when I start another activity and start it back in onResume ?

Here is my code for the thread which updates the ListView of the current Activity.

private void runEventHandler() {
    new Thread() {
        public void run() {
            while (true) {
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            users.add(new User(10, "a", false));
                            adapter.notifyDataSetChanged();
                        }
                    });
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

Now I must be able to update the child activities' ListViews when a new User is added.

Upvotes: 0

Views: 1297

Answers (2)

pouyan
pouyan

Reputation: 3449

one possible way is that you create a flag in both activity to control your threads to be run ( the following codes are not runable just example to understand what you can do):

Activity A
{
public static boolean stopThread = false;
    @Override
    public void onResume()
    {
     super.onResume();
    // put your code here...
     stopThread =false;
     runEventHandler();
     }
private void runEventHandler() {
new Thread() {
    public void run() {
        while (A.stopThread != false) {
            try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        users.add(new User(10, "a", false));
                        adapter.notifyDataSetChanged();
                    }
                });
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}.start();
}


  @Override
protected void onStop(){
    super.onStop();
    stopThread =true;
   }

}



Activity B
{
public static boolean stopThread = false;
    @Override
    public void onResume()
    {
     super.onResume();
    // put your code here...
     stopThread =false;
     runEventHandler();
     }
private void runEventHandler() {
new Thread() {
    public void run() {
        while (B.stopThread != false) {
            try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        users.add(new User(10, "a", false));
                        adapter.notifyDataSetChanged();
                    }
                });
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}.start();
}


  @Override
protected void onStop(){
    super.onStop();
    stopThread =true;
   }

}

also you can use onPause() instead of onStop(). depends on your program concept.

Upvotes: 1

Lucas Ferraz
Lucas Ferraz

Reputation: 4152

You should a timertask like in link below : https://examples.javacodegeeks.com/android/core/activity/android-timertask-example/

the code to post any change on UIThread if you are in a different thread like doInBackground of AsyncTask:

runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // Here you can set your Ui Components
                            }
                        });

Upvotes: 1

Related Questions