It took my quite a while to get this to work, but It's clearly not best practice. In short, I need to show a dialog when my AsyncTask finishes, but getApplicationContext() does not work, neither does passing it as a parameter when creating the AsyncTask. So I've declared a public variable for the context in my AsyncTask class and set it before I execute:
    private OnClickListener clickLoadRefs = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("H","Clicked Load Refs");
        RefreshRefPoints refreshRefPoints = new RefreshRefPoints();
        refreshRefPoints.myCtx=v.getContext();
        refreshRefPoints.execute(v.getContext());
    }
};
private class RefreshRefPoints extends AsyncTask<Context, Integer, Integer> {
    private Integer nPoints=0;
    public Context myCtx;
    private ProgressDialog pd;
    protected Integer doInBackground(Context... ctx) {
        Log.d("H","doInBackground()");
        dbHelper.clearRefPoints();
        requestRefPoints();
        nPoints = parseRefPointsCSV();
        return nPoints;
    }
    protected void onProgressUpdate(Integer... progress) {
    }
    protected void onPreExecute() 
    {
        pd = ProgressDialog.show(myCtx, "Refreshing Reference Points", "Loading...", true,false);
        Log.d( "H", "onPreExecute()" );
    }
    protected void onPostExecute(Integer result) {
        pd.dismiss();
        AlertDialog.Builder builder = new AlertDialog.Builder(myCtx);
        builder.setTitle("Reference points refresh complete");
        builder.setMessage(result+" records loaded");
        builder.setPositiveButton("OK",null);
        builder.show();
        Log.d("H","onPostExecute()");       
    }...etc
Can anybody just show me the proper way of passing the context?
Thanks
Define a constructor method and pass context a parameter. It would be better.
Here what I meant:
private class RefreshRefPoints extends AsyncTask<Void, Integer, Integer> {
    private Integer nPoints=0;
    private Context myCtx;
    private ProgressDialog pd;
    public RefreshRefPoints(Context ctx){
        // Now set context
        this.myCtx = ctx;
    }
}
That's all.
You may also use YourActivityName.this to refer to the activity Context. Because Activites extend Context, so its valid to do so.
Pass context in constructor as
private OnClickListener clickLoadRefs = new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("H","Clicked Load Refs");
        RefreshRefPoints refreshRefPoints = new RefreshRefPoints(Your_ActivityName.this);
        refreshRefPoints.myCtx=v.getContext();
        refreshRefPoints.execute(v.getContext());
    }
};
private class RefreshRefPoints extends AsyncTask<Void, Integer, Integer> {
    private Integer nPoints=0;
    public Context myCtx;
    private ProgressDialog pd;
public RefreshRefPoints (Context ctx) {
    myCtx = ctx;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With