Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>AsyncTask</code> help you do an asynchronous job. In your code, I can see you return <code>Drawable</code> right after calling it. But at that moment, the your asynctask hasn't completed yet and drawable still null.</p> <pre><code>task2.execute(src); return profile; </code></pre> <p>If you want set drawable resource when complete job in asynctask, just put your <code>ImageView</code> into your method. It should be:</p> <pre><code> public void getProfile(String src, final ImageView v){ @Override protected void onPostExecute(Drawable result2) { // set drawable for ImageView when complete. v.setImageDrawable(result2); dialog2.dismiss(); } task2.excute(src); //do not need return anything. } </code></pre> <p>Use it:</p> <pre><code> ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData); getProfile("http://..../xyz.jpg", thoughtsProfilePic ); </code></pre> <p>Hope this help.</p> <p><strong>Update</strong>:<br> There is no way to return value from asynchronous method directly, here is another choice. First, create an interface to notify when complete job.</p> <pre><code> public interface INotifyComplete{ public void onResult(Drawable result); } </code></pre> <p>Then your activity class should look like:</p> <pre><code> public class YourActivity extends Activity implement INotifyComplete{ private Drawable res1; private Drawable res2; public void onResult(Drawable result){ if(result == res1){ // do something with resource 1 ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData); thoughtsProfilePic.setImageDrawable(result); } if(result == res2){ // do something with resource 2 } } void someMethod(){ // you can use this way to call getProfile("http://..../xyz.jpg", res1, YourActivity.this); //or this getProfile("http://..../xyz.jpg", res2, new INotifyComplete(){ public void onResult(Drawable result){ // result is res2, so don't need to check } }); } public void getProfile(String src, final Drawable res, final INotifyComplete notify){ //don't need store asynctask instance AsyncTask&lt;String, Void, Drawable&gt; task2 = new AsyncTask&lt;String, Void, Drawable&gt;(){ // do something ... protected void onPostExecute(Drawable result2) { dialog2.dismiss(); // you will set the value to your drawable then notify it when complete job res = result2; notify.onResult(res); } } task2.excute(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload