Java – How to call notifyDataSetChanged() from AsyncTask

How to call notifyDataSetChanged() from AsyncTask… here is a solution to the problem.

How to call notifyDataSetChanged() from AsyncTask


have an ArrayAdapter with an AsyncTask in it, but I’m not sure how to call notifyDataSetChanged/ from onPostExecute<


public class ColorAdapter extends ArrayAdapter<Color> {

List<Color> colorList;
  Context context;

public ColorAdapter(Context context, List<Color> list) {
    this.context = context; this.colorList = list;  

public View getView (final int position, final View view, final ViewGroup parent) { 

class DeleteColorTask extends AsyncTask <String, String, String> {
   int colorId;
   DeleteColorTask (int colorId) {this.colorId = colorId; } 

protected String doInBackgroud (String ... args) {
     call to server to delete the color
   protected void onPostExecute(String s) {
     HOW CAN I CALL notifyDataSetChanged() here?? this.notifyDataSetChanged() doesn't work since I am inside DeleteColorTask class

I call the above from my activity like this:

  adapter = new ColorAdapter(context, colorsList);


You can call it like this:


By the way, a more appropriate place to start this AsyncTask is its host fragment/activity, why?
AsyncTasks can sometimes stay longer than you expect, and they can be a nuisance if you don’t manage their lifecycle locally.

Related Problems and Solutions