I have an implementation of a Fragment
class.The class is supposed to return a view
of a graph plotted dynamically using data from a remote server.Everything is working ok.But now i want to implement an AsyncTask
class to increase responsiveness of the app.Problem is how do i return an ArrayList
to the calling class.I can't seem to locate a good example from the internet.
Here is my method which creates and returns the view:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newfragment, container, false);
sharename = getArguments().getString(EXTRA_SHARENAME);
// performance = getPerformance(sharename);
// new GraphSharePerformance().execute();
new GraphSharePerformance() {
@Override
protected void onPostExecute(ArrayList<SharePerformance> param) {
super.onPostExecute(param);
ArrayList<SharePerformance> results = param;
performance = param;
// USE THE RESULT here
}
}.execute();
LinearLayout layout = (LinearLayout) v.findViewById(R.id.chart);
layout.addView(displayLineChart(performance));
return v;
}
And this is my AsyncTask
class:
protected class GraphSharePerformance extends
AsyncTask<GraphFragment, Void, ArrayList<SharePerformance>> {
ArrayList<SharePerformance> shareperformance = new ArrayList<SharePerformance>();
protected void onPreExecute() {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("loading");
progressDialog.show();
super.onPreExecute();
}
@Override
protected ArrayList<SharePerformance> doInBackground(
GraphFragment... params) {
shareperformance = getPerformance(sharename);
return shareperformance;
}
protected void onPostExecute(ArrayList<SharePerformance> param) {
progressDialog.dismiss();
}
}
Now how do i get the returned ArrayList
?I have tried using a global variable to hold the arraylist
but this doesn't work even though it has always worked in other classes.The graph does not plot any value.I will appreciate help on this.Thank you.