Reputation: 9049
package de.tablayoutexample;
import java.util.List;
import android.os.AsyncTask;
import android.util.Log;
public class CallWebServiceTask extends AsyncTask<String, String, String> {
private PhotosActivity theActivity;
public CallWebServiceTask(PhotosActivity theActivity){
this.setTheActivity(theActivity);
}
@Override
protected void onPreExecute(){
PhotosActivity.showSpinner();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return PhotosActivity.getEventList();
}
@Override
protected void onPostExecute(String result) {
List<SingleEvent> thelist = PhotosActivity.parseJSONResponse(result);
/*for(int i=0;i<thelist.size();i++){
SingleEvent entry = thelist.get(i);
Log.v("Finished", entry.getTitle());
}*/
theActivity.refreshListView(thelist);
PhotosActivity.hideSpinner();
}
public PhotosActivity getTheActivity() {
return theActivity;
}
public void setTheActivity(PhotosActivity theActivity) {
this.theActivity = theActivity;
}
}
I'm not entirely sure what I am doing, but I ended up writing the class above that works with my PhotosActivity class, which contains all the necessary methods that are called. However, what if I wanted to create an asynctask class that could be used for all of my activities? How would I do that? I tried changing all the PhotosActivity types to "Activity" but that doesnt seem to be all there is to it.
My question is, how would I make this class truly dynamic?
EDIT:
Here is my CustomActivity base class that includes the AsyncTask class inside:
package de.tablayoutexample;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
public class CustomActivity extends Activity{
public String getEventList() {
// TODO Auto-generated method stub
return null;
}
public Object parseJSONResponse(String result) {
// TODO Auto-generated method stub
return null;
}
public void showSpinner() {
// TODO Auto-generated method stub
}
public void refreshListView(Object thelist) {
// TODO Auto-generated method stub
}
public void hideSpinner() {
// TODO Auto-generated method stub
}
public class CallWebServiceTask extends AsyncTask<String, String, String> {
private CustomActivity theActivity;
public CallWebServiceTask(CustomActivity theActivity){
this.setTheActivity(theActivity);
}
@Override
protected void onPreExecute(){
theActivity.showSpinner();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.i("ADASD","EXECUTED");
return theActivity.getEventList();
}
@Override
protected void onPostExecute(String result) {
Object thelist = theActivity.parseJSONResponse(result);
theActivity.refreshListView(thelist);
theActivity.hideSpinner();
}
public CustomActivity getTheActivity() {
return theActivity;
}
public void setTheActivity(CustomActivity theActivity) {
this.theActivity = theActivity;
}
}
}
So everything is working correctly except for the parseJSONResponse method because the return type will be different for every Activity that uses this base class:
For example, my PhotosActivity returns a List of my SingleEvent object, which gson nicely makes for me.
public List<SingleEvent> parseJSONResponse(String jsonResponse) {
//using gson, place all the json into the SingleEvent object and then into a List
Type listType = new TypeToken<List<SingleEvent>>(){}.getType();
List<SingleEvent> events = new Gson().fromJson(jsonResponse, listType);
return events;
}
The problem is that the next Activity will use a different object to store my values. I don't know how to deal with the different object types.
Upvotes: 0
Views: 965
Reputation: 5786
I have two things to say:
1) Consider using interfaces. By having your Activities implement a particular interface, you can use them interchangeably.
2) That being said, watch out with what you're doing in your AsyncTask... make sure that whatever you do in your doInBackground method doesn't access code that's being used by the UI thread.
Upvotes: 1
Reputation: 17332
Be careful with the terms. "Dynamic" is a loaded word. this is basic OOD. Make the static methods of your PhotosActivity regular methods, and create a base class that all of your activities extend. Have a "doWork" method in those activities that the AsyncTask calls.
there are better ways to do this kind of thing, but if its what you're looking for, it'll work.
Upvotes: 1