i know it should be context.
what exactly is a context though.
usually when i create a dialog in a class
i do something like this:
final Dialog dialog = new Dialog(this);
but now i am trying to create a dialog in an AsyncTask<>
therefore i cannot do the above cause AsyncTask isn’t a context evidently.
the AsyncTask is a class in itself, which is to say its not a subclass right now.
public class popTask extends AsyncTask<Void, Void, String> {
Context con =
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
final Dialog dialog = new Dialog(con);
dialog.setContentView(R.layout.custom);
dialog.setTitle("New & Hot advertise");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.yoda);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
The following are the two ways you can send your context from the activity where you are executing your AsyncTask:
In your
popTaskcreate a private variable where you can set your context.In the first option you need to have a constructor for your class
popTaskwhere you are accepting a context.For the second option if you are not passing anything meaningful to the function
doInBackground()you can change the following line:}
you will receive the context object in the
doInBackground()which you can set in the private Context variable of thepopTaskclass and then access it in thedoInBackground()function.