-1

When my AsyncTask finishes, I display info in a TextView.

    @Override protected void onPostExecute(Void x)
    {
      MainActivity.lblLastLetterProcessed.setText("A-Z loaded");
    }

Like so:

  public static void setLastLetterProcessed(char c){
    lblLastLetterProcessed.setText("" + c);
  }

I'd rather give the info via Toast but Toast requires Context.

protected void onPostUpdate(Integer... progress) 
{
  Toast toast= Toast.makeText(???????????, "Loaded A-Z", Toast.LENGTH_LONG);
  toast.show();
}

So I wrote popupMessage:

  void popupMessage(String text)
  {
    SpannableStringBuilder altText = new SpannableStringBuilder(text);
    altText.setSpan(new ForegroundColorSpan(0xFFCC5500), 0, altText.length(), 0);
    int duration = Toast.LENGTH_SHORT;
    Context context = getApplicationContext();
    Toast toast = Toast.makeText(context, altText, duration);
    toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
    toast.show();
  }

But I can't call it from onPostUpdate since popupMessage is NOT static.

If I make it static, I can't use getApplicationContext. And if I add Context as a parameter, I can't pass Context from onPostUpdate.

Googling has provided no SO Answers I can implement.

What can I do?

DSlomer64
  • 4,234
  • 4
  • 53
  • 88

3 Answers3

3
public class MyTask extends AsyncTask<Void, Void, Void>{
    private Context mContext;

    public MyTask(Context context){
        mContext = context;
    }

    ...
    ... 

    protected void onPostUpdate(Integer... progress){
        Toast toast= Toast.makeText(mContext, "Loaded A-Z", Toast.LENGTH_LONG);
        toast.show();
    }
}

and instantiate it like so:

MyTask task = new MyTask(MyActivity.this).execute();

This is how I do AsyncTask's that require arguments, I also define callbacks this way in the constructor of the Tasks to interact with the activities that call them.

Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25
0

Thanks to @Lucas Crawford, this works:

public class ListMaker extends ArrayList<String> 
{
  Context mContext; //////////////////////////////
  ...    

  public ListMaker(AssetManager assets, Context c){
    //                                  ^^^^^^^^^^
    ...    
    mContext = c;  /////////////////////////////////

    LoadWords loadWords = new LoadWords();
    ...
    loadWords.execute((Object []) null);
  }

  private class LoadWords extends AsyncTask<Object, Integer, Void>
  {
    @Override protected Void doInBackground(Object... params) 
    {
       ...
       publishProgress(...);
    }

    @Override protected void onPostExecute(Void x)
    {
      Toast toast= Toast.makeText(mContext, "Loaded A-Z", Toast.LENGTH_LONG);
       //                         ^^^^^^^^
      toast.show();

    }
    ...
  }
}

And even better I have an improved, more useful popupMessage that I can call from onPostExecute like so:

     MainActivity.popupMessage("Loaded A-Z", mContext);

And here it is:

  public static void popupMessage(String text, Context context)
  //     ******                                **********
  {
    SpannableStringBuilder altText = new SpannableStringBuilder(text);
    altText.setSpan(new ForegroundColorSpan(0xFFCC5500), 0, altText.length(), 0);
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, altText, duration);
    toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
    toast.show();
  }
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • It kind of irritates me when I see how easy something is, why didn't I think of that, but I guess that's how we learn.... – DSlomer64 Sep 09 '15 at 19:01
0

I expect you have the async method in a seperate Class

package nl.yentel.finekinney;

import android.app.Activity;
import android.os.AsyncTask;
import android.view.Gravity;
import android.widget.Toast;

public class Demo extends AsyncTask<String, Void, String> {
     Activity demoActivity; //this is a public variable

public void ActivityName(Activity activity) {
    //calling this void will set the variable demoActivity to the input activity
    demoActivity = activity;
}

@Override
protected String doInBackground(String... strings) {
    return null;
}

@Override
protected void onPreExecute() {
}

@Override
protected void onPostExecute(String s) {
    Toast toast = Toast.makeText(demoActivity, "the text in the toast", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();
}
}

Then I have to call this Class

public void openMail(final View view) {
    //first create a pointer to the Demo Class
    Demo JsonConnect = new Demo();
    //then attach the context activity to the ActivityName

    //in the Class 'demoActivity' will point to the calling activity;
    JsonConnect.ActivityName(this);
    JsonConnect.execute();
}

Now the toast will point to the proper calling activity

Toast toast = Toast.makeText(demoActivity, "the text in the toast", Toast.LENGTH_LONG);
frans eilering
  • 449
  • 4
  • 6