Rascalsailor
Newbie
Hi all. I wrote a simple weather app which goes fetches data from Openweathermap's API in Json format. I used an Aynctask to fetch the data, using ideas from online (as we all do!)
All works ok, but I am trying to get a better understanding of the callback implementation (similar to the one at http://smartandroidians.blogspot.com/2015/03/how-to-use-callback-or-interface-pass.html)
In particular, I am trying to understand how is the interface is implemented without the keyword 'implements' anywhere?
The Async thread:
And in the main thread:
Then in the code that I did:
Thanks all
Russell
All works ok, but I am trying to get a better understanding of the callback implementation (similar to the one at http://smartandroidians.blogspot.com/2015/03/how-to-use-callback-or-interface-pass.html)
In particular, I am trying to understand how is the interface is implemented without the keyword 'implements' anywhere?
The Async thread:
Java:
class MyTask extends AsyncTask {
public interface OnUpdateListener {
public void onUpdate(MyObject obj);
}
OnUpdateListener listener;
MyTask() {
}
public void setUpdateListener(OnUpdateListener listener) {
this.listener = listener;
}
MyObject doInBackground() {
return obj;
}
onPostExecute(MyObject obj) {
if (listener != null) {
listener.onUpdate(obj);
}
}
Java:
yActivity extends Activity {
void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTask task = new MyTask();
task. setUpdateListener(new MyTask. OnUpdateListener() {
public void onUpdate(MyObject obj) {
.....
}
});
task.execute();
}
}
Then in the code that I did:
Java:
public class fetchData extends AsyncTask<Void, String, String> {
//------------added--------------
public interface AsyncResponse {
void processFinish(String output);
}
public AsyncResponse delegate = null;//Call back interface
public fetchData(AsyncResponse asyncResponse) {
delegate = asyncResponse;//Assigning call back interface through constructor
}
//---------------------------------------
String data = "";
String dataParsed = "";
String singleParsed = "";
String iconValue = "";
protected String doInBackground(Void...voids) {
try {
Thanks all
Russell