java - How can i return variable "realImage" which is within onComplete method? -


i've created asynctask in activity , want return variable "realimage" asynctask cant seem access it...

public class photoutils { public static photo getimage(string id) {     unsplash.getphoto(id, new unsplash.onphotoloadedlistener()     {         @override         public void oncomplete(photo photo)         {             photo realimage=photo;         }          @override         public void onerror(string error)         {          }     });      return realimage; //this line shows error cannot resolve symbol                          realimage } } 

this async task in other activity

public class imagetask extends asynctask<photo,void,photo> {      @override     protected photo doinbackground(photo... photos)     {             intent intent=getintent();              bundle bd=intent.getextras();              string getid = (string) bd.get("id");              photo finalphoto=photoutils.getimage(getid);              return finalphoto;     }      @override     protected void onpostexecute(photo finalphoto) {         tv1.settext(finalphoto.getid());         super.onpostexecute(finalphoto);     } } 

first, looks don't need asynctask. look @ example code

secondly, can't return that.

onphotoloadedlistener asynchronous callback.

just use image normal within oncomplete.

if needed pass image calling method, extract callback parameter.

// void  public static void getimage(string id, unsplash.onphotoloadedlistener listener) {     unsplash.getphoto(id, listener); }); 

call as

photoutils.getimage(id, new unsplash.onphotoloadedlistener() {     // use image in here  }); 

which, if closely, did replace normal usage of unsplash.getphoto photoutils.getimage, may want want, accomplish similar approach singleton pattern around unsplash instance variable. way, you're not rewriting of unsplash api calls


Comments