I’ve been using Callable, but now I need the function to use a param in the call method. I get that this isn’t a capability of call so how can I do this?
What I currently have (wrong):
AsyncTask async = new MyAsyncTask();
async.finished(new Callable(param) {
// the function called during async.onPostExecute;
doSomething(param);
});
async.execute(url);
MyAsyncTask:
...
@Override
protected void onPostExecute(JSONObject result) {
//super.onPostExecute(result);
if(result != null) {
try {
this._finished.call(result); // not valid because call accepts no params
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void finished(Callable<Void> func) {
this._finished = func;
}
...
If you make
parama final variable, you can just refer to it from within theCallable:You’ll have to do this when you create the
Callablethough – you can’t give it the value later. If you need that for some reason, you’ll have to basically use shared state – some “holder” which theCallablehas access to, and which can have the value set into it before theCallableexecutes. That could probably just be theMyAsyncTaskitself:Then: