将 AsyncTask 结果传递给 Fragment 中的 onCreateView
Pass AsyncTask result to onCreateView in Fragment
这是我的情况:
在我的 onCreate() 中,我想创建一个 AsyncTask 来检索 WebService 的数据并将其存储在 ArrayList(Retrive points for graph) 中。
然后在我的 onCreateView() 中,我想使用 ArrayList 并迭代它以绘制点...
我想要一些方法来确保在启动 onCreateView() 函数之前等待 AsyncTask 完成执行。然后我在 onPostExecute 中使用了回调,但它不起作用。我阅读了有关在任务上使用 get() 的信息,但是如果我使用 get() 方法 UI 将是 blocked.So 在 onPostExecute 中使用回调方法的唯一可能方法。
但是我的代码是这样的 运行 在完成 Aysnctask onCreateView 执行之前并在此行获取 NullPointer 异常 if(PF.size() != 0).
package hello.aws.graphs;
import android.support.v7.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import hello.aws.login.WebService;
public class PFGraph extends AppCompatActivity implements AsyncTaskCompleteListener {
public static boolean errored = false;
static ProgressDialog dialog = null;
public static Menu menu;
static ArrayList<PFKVABean> PF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
getData();/*Getting data from webservice**/
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newIntent;
switch (item.getItemId()) {
case R.id.bb_menu_favorites:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recent:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recents:
getData();/*Getting data from webservice**/
break;
}
return true;
}
public void getData()
{
AsyncCallWS task = new AsyncCallWS(this);
task.execute();
}
@java.lang.Override
public void onTaskComplete(Object result) {
PF=(ArrayList)result;//Storing result of AsyncTask
Log.e("check","check");
}
/**
* A fragment containing a line chart.
*/
public static class PlaceholderFragment extends Fragment {
private LineChartView chart;
private LineChartData data;
private int numberOfLines = 2;
private boolean hasLines = true;
private boolean hasPoints = true;
private ValueShape shape = ValueShape.CIRCLE;
private boolean isFilled = false;
private boolean hasLabels = false;
private boolean isCubic = false;
private boolean hasLabelForSelected = false;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_pf_graph, container, false);
chart = (LineChartView) rootView.findViewById(R.id.chart);
chart.setOnValueTouchListener(new ValueTouchListener());
generateData();
return rootView;
}
private void generateData() {
chart.setZoomEnabled(true);
chart.setZoomType(ZoomType.HORIZONTAL);
List<Line> lines = new ArrayList<Line>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();
if(!errored) {
if (PF.size() != 0) {/******NULL Pointer Exception***/
/****Here the logic for plotting the points on graph***/
}
else
{
Toast.makeText(getActivity(), "Error occured in invoking Webservice", Toast.LENGTH_LONG).show();
}
}
}
}
private class AsyncCallWS extends AsyncTask<String, Void, ArrayList> {
private AsyncTaskCompleteListener<ArrayList> callback;
public AsyncCallWS(AsyncTaskCompleteListener<ArrayList> cb) {
this.callback = cb;
}
@java.lang.Override
protected ArrayList doInBackground(String... params) {
PF=WebService.GetWSData();
return PF;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(PFGraph.this, R.style.MyTheme);
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.cp_bar));
dialog.setCancelable(false);
dialog.show();
// getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
}
@Override
protected void onPostExecute(ArrayList PF) {
if(dialog!=null||dialog.isShowing()) {
dialog.dismiss();
Log.e("v","v");
callback.onTaskComplete(PF);//Sending result
}
}
}
}
将对 generateData();
的调用从 onCreateView()
移动到 onTaskComplete()
,以便仅在数组填充后调用它。
这是我的情况: 在我的 onCreate() 中,我想创建一个 AsyncTask 来检索 WebService 的数据并将其存储在 ArrayList(Retrive points for graph) 中。
然后在我的 onCreateView() 中,我想使用 ArrayList 并迭代它以绘制点...
我想要一些方法来确保在启动 onCreateView() 函数之前等待 AsyncTask 完成执行。然后我在 onPostExecute 中使用了回调,但它不起作用。我阅读了有关在任务上使用 get() 的信息,但是如果我使用 get() 方法 UI 将是 blocked.So 在 onPostExecute 中使用回调方法的唯一可能方法。
但是我的代码是这样的 运行 在完成 Aysnctask onCreateView 执行之前并在此行获取 NullPointer 异常 if(PF.size() != 0).
package hello.aws.graphs;
import android.support.v7.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import hello.aws.login.WebService;
public class PFGraph extends AppCompatActivity implements AsyncTaskCompleteListener {
public static boolean errored = false;
static ProgressDialog dialog = null;
public static Menu menu;
static ArrayList<PFKVABean> PF;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
getData();/*Getting data from webservice**/
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent newIntent;
switch (item.getItemId()) {
case R.id.bb_menu_favorites:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recent:
getData();/*Getting data from webservice**/
break;
case R.id.bb_menu_recents:
getData();/*Getting data from webservice**/
break;
}
return true;
}
public void getData()
{
AsyncCallWS task = new AsyncCallWS(this);
task.execute();
}
@java.lang.Override
public void onTaskComplete(Object result) {
PF=(ArrayList)result;//Storing result of AsyncTask
Log.e("check","check");
}
/**
* A fragment containing a line chart.
*/
public static class PlaceholderFragment extends Fragment {
private LineChartView chart;
private LineChartData data;
private int numberOfLines = 2;
private boolean hasLines = true;
private boolean hasPoints = true;
private ValueShape shape = ValueShape.CIRCLE;
private boolean isFilled = false;
private boolean hasLabels = false;
private boolean isCubic = false;
private boolean hasLabelForSelected = false;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_pf_graph, container, false);
chart = (LineChartView) rootView.findViewById(R.id.chart);
chart.setOnValueTouchListener(new ValueTouchListener());
generateData();
return rootView;
}
private void generateData() {
chart.setZoomEnabled(true);
chart.setZoomType(ZoomType.HORIZONTAL);
List<Line> lines = new ArrayList<Line>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();
if(!errored) {
if (PF.size() != 0) {/******NULL Pointer Exception***/
/****Here the logic for plotting the points on graph***/
}
else
{
Toast.makeText(getActivity(), "Error occured in invoking Webservice", Toast.LENGTH_LONG).show();
}
}
}
}
private class AsyncCallWS extends AsyncTask<String, Void, ArrayList> {
private AsyncTaskCompleteListener<ArrayList> callback;
public AsyncCallWS(AsyncTaskCompleteListener<ArrayList> cb) {
this.callback = cb;
}
@java.lang.Override
protected ArrayList doInBackground(String... params) {
PF=WebService.GetWSData();
return PF;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(PFGraph.this, R.style.MyTheme);
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.cp_bar));
dialog.setCancelable(false);
dialog.show();
// getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlaceholderFragment()).commit();
}
@Override
protected void onPostExecute(ArrayList PF) {
if(dialog!=null||dialog.isShowing()) {
dialog.dismiss();
Log.e("v","v");
callback.onTaskComplete(PF);//Sending result
}
}
}
}
将对 generateData();
的调用从 onCreateView()
移动到 onTaskComplete()
,以便仅在数组填充后调用它。