How can I reuse an inner class in Java / Android?
android, android-asynctask, java
Solution
Java doesn't have something like C# delegates, so your first proposal is not possible as-is.
The standard solution is to declare an interface that can be passed as a callback:
public interface MyCallback {
void gotItems(String data);
}
Then you can do something like
class ActivityA extends Activity {
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//get stuff and use the onPostExecute inside the APIWrapper
new APIWrapper().getItems("Category A", new MyGetItemsCallBack());
}});
private class MyGetItemsCallBack implements MyCallback {
public void gotItems(String result) {
// bla bla bla
}
}
}
or you can use an anonymous class:
class ActivityA extends Activity {
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//get stuff and use the onPostExecute inside the APIWrapper
new APIWrapper().getItems("Category A", new MyCallback() {
public void gotItems(String result) {
// bla bla bla
}
});
}});
If you need many different kinds of callbacks you can use generics to avoid having do declare many small interfaces:
interface Callback<T> {
void run(T arg);
}
class ActivityA extends Activity {
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//get stuff and use the onPostExecute inside the APIWrapper
new APIWrapper().getItems("Category A", new Callback<String>() {
public void run(String result) {
// bla bla bla
}
});
}});
Problem
I'm a long time (8 years) C# developer dabbling in a little android development. This is the first time I've used Java and am having a little trouble shifting my mindset when it comes to inner classes. I'm trying to write a class to wrap an RESTful API to execute various calls on the server from one typesafe place, e.g. ``` string jsonResult = APIWrapper.getItems("category 1"); ``` And I want to use a AsyncTask to get stuff in the background. So one way is this - have the AsyncTask in the APIWrapper : ``` class ActivityA extends Activity { myButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { //get stuff and use the onPostExecute inside the APIWrapper new APIWrapper().GetItems("Category A", MyGetItemsCallBack); }}); function void MyGetItemsCallBack(String result) { //render the result in the activity context, in a listview or something } } ``` I'm not sure the callback / delegate idea works in Java anyway! The other way is to have the AsyncTask in the Activity and create the APIWrapper to just get the data like a worker / helper: ``` class ActivityA extends Activity { myButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { //get stuff and use the onProcessComplete inside the APIWrapper new GetItems("Category A"); }}); class GetItems(String theCategory) extends AsyncTask { doInBackground() { return new APIWrapper().GetItems(theCategory); } onPostExecute(String result) { //render the result in the activity context, in a listview or something } } } ``` Can anyone help me make the right choice?