我在viewPager中使用片段。片段之一使用asynctask来下载数据。现在的问题是任务还没有完成,但片段的onCreateView已经返回。 任务完成后我可以更新片段吗?我试图调用view.invalidate()不起作用。
public class LineChartFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
new Background().execute();
return view;
}
}
class Background extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
if (haveInternet()) {
getter();
}
return null;
}
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//UPDATE fragment
....
view = ChartFactory.getLineChartView(getActivity().getApplicationContext(), dataset, renderer);
....
view.invalidate();
}