0

我想完全隐藏在执行 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);
4

2 回答 2

0

我还看到您的代码在正常对话框后面出现了一些奇怪的对话框。尝试创建一个单独的 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();
于 2012-06-24T22:18:08.710 回答
0

我找到了这个问题的原因:它的事实是 ListFragment 的 ListAdapter 尚未设置。解决方案是将 ListAdapter 设置为一个空列表,它告诉 ListFragment 它有结果并且没有进行其他处理。

通过将以下 2 行添加到 onCreate 方法解决了问题

ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(getActivity(), R.layout.productlist_item, new ArrayList<Product>());
setListAdapter(adapter);
于 2012-06-30T01:46:25.663 回答