[英]Android Asynctask displaying circle behind the progress dialog
我想完全隐藏在执行AsyncTask时显示的“进度”圆圈(下面的代码)-同时显示进度对话框。 问题在于可以通过透明进度对话框看到圆圈。 有人可以帮忙吗? 我不希望更改“进度”对话框的透明度...
new AsyncTask<String, Void, List<Product>>() {
private ProgressDialog dialog;
protected void onPreExecute() {
this.dialog = ProgressDialog.show(getActivity(), "Progress", "Retrieving products...", false, false);
}
protected void onPostExecute(List<Product> result) {
// populate the list
Context context = getSherlockActivity().getApplicationContext();
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(context, R.layout.productlist_item, result);
setListAdapter(adapter);
// dismiss the progress dialog
dialog.dismiss();
}
@Override
protected List<Product> doInBackground(String... params) {
// populate the database if required
try {
// get the data from the server
MyApplication.populateProductDatabase(false);
} catch (NotConnectedToInternetException e) {
exceptionMessage = "Unable to retrieve data. Internet connection unavailable.";
e.printStackTrace();
}
// get the records in the database
ProductDAO dao = new ProductDAO();
List<Product> result = dao.getProducts(params[0]);
return result;
}
}.execute(find);
我还看到了普通代码背后的一些奇怪的对话框。 请尝试制作一个单独的AsyncTask类。
就像是:
private class MyAsyncTask extends AsyncTask<String, Void, List<Product> {
private Activity mActivity;
private ProgressDialog mDialog;
public MyAsyncTask(){
this.mActivity = MyActivity.this;
mDialog = new ProgressDialog(mActivity);
}
protected void onPreExecute() {
mDialog.setMessage("Retrieving products...");
mDialog.show();
}
protected void onPostExecute(List<Product> result) {
// populate the list
Context context = getSherlockActivity().getApplicationContext();
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(context, R.layout.productlist_item, result);
setListAdapter(adapter);
// dismiss the progress dialog
mDialog .dismiss();
}
@Override
protected List<Product> doInBackground(String... params) {
// populate the database if required
try {
// get the data from the server
MyApplication.populateProductDatabase(false);
} catch (NotConnectedToInternetException e) {
exceptionMessage = "Unable to retrieve data. Internet connection unavailable.";
e.printStackTrace();
}
// get the records in the database
ProductDAO dao = new ProductDAO();
List<Product> result = dao.getProducts(params[0]);
return result;
}
}
然后调用:
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask .execute();
我发现了此问题的原因:尚未设置ListFragment的ListAdapter的事实。 解决方案是将ListAdapter设置为一个空列表,该列表告诉ListFragment它具有它的结果,并且其他处理没有发生。
通过将以下两行添加到onCreate方法中,解决了该问题
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(getActivity(), R.layout.productlist_item, new ArrayList<Product>());
setListAdapter(adapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.