1

i have this class:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

ImageFromWeb ifw;
private String url;
private final WeakReference<ImageView> imageViewReference;

public DownloadImageTask(ImageView imageView) {
    imageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground(String... params) {
    url = params[0];
    try {
        return BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(Bitmap result) {
    if (isCancelled()) {
        result = null;
    }
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(result);
        }
    }
}

@Override
protected void onPreExecute() {
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
 ---------> imageView.setImageResource(R.drawable.pw);     
        }
    }
}

}

and the main activity:

public class ImageFromWeb extends Activity {

private String path = "http://....";
private ImageView imageView;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main);

    ImageView mChart = (ImageView) findViewById(R.id.imview);

    mChart.setTag(path);
    new DownloadImageTask(mChart).execute(path);
}

}

I want to put in the point of arrow(in DownloadImageTask class) an alert dialog! How can i do this? Because this class isn't an activity.

thanks :)

John
  • 25
  • 1
  • 3

4 Answers4

4

change the constructor and pass a Context object

Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    this.mContext = mContext;
}

Now you can use this Context to create dialogs

You can even cast mContext to your Activity class and call functions within your Activity

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • i did this and in the line "new DownloadImageTask(mChart).execute(path);", i put this "new DownloadImageTask(mChart, this.getApplicationContext()).execute(path);"! Is this right? i have an error with this way! – John Aug 16 '11 at 11:26
  • [update] i put in the onCreate method, "Context mContext = this;" and after "new DownloadImageTask(mChart, mContext).execute(path);" it works! thanks a lot! – John Aug 16 '11 at 11:29
0

You can have a static Context in your Application like this:

public static Context CurrentContext;

and a custom abstract Activity that sets currentContext upon creation like this:

public abstract class CustomActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.CurrentContext = this;
    }
}

Then you would get context like this:

AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
                    dlgBuilder.setTitle("Context Example");
                    dlgBuilder.setMessage("I am being shown from the application Static context!");
                    dlgBuilder.setNeutralButton("Ok", null);
                    dlgBuilder.show();

This way you never have to worry about context wether you are in a background task or directly in an Activity it will work for most cases.

hope this helps!

Jmorvan
  • 1,020
  • 11
  • 31
0

Move the Async Task to your activity and use that to call your DownloadImageTask class & methods. This will make your life a lot easier.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
0

pass a Activity instance to the class where you want to display dialog, and check

if(!actvity.isFinishing){
//show dialog
}
ngesh
  • 13,398
  • 4
  • 44
  • 60