我有一个类的实现Fragment
。该类应该返回view
使用来自远程服务器的数据动态绘制的图形。一切正常。但是现在我想实现一个AsyncTask
类来提高应用程序的响应能力。问题是如何我要返回ArrayList
调用类吗?我似乎无法从互联网上找到一个很好的例子。这是我创建并返回视图的方法:
@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;
}
这是我的AsyncTask
课:
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();
}
}
现在我如何获得返回的ArrayList
值?我尝试使用全局变量来保存,arraylist
但这不起作用,即使它一直在其他类中工作。图表没有绘制任何值。我将不胜感激。谢谢你。