Android Asynctask在进度对话框后面显示圆圈

时间:2012-06-24 21:38:01

标签: android

我想完全隐藏在执行AsyncTask(下面的代码)期间显示的Progress圆圈 - 同时显示进度对话框。问题是可以通过透明进度对话框看到圆圈。有人可以帮忙吗?我不想改变进度对话框的透明度......

    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);

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

通过在onCreate方法

中添加以下两行来解决问题
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(getActivity(), R.layout.productlist_item, new ArrayList<Product>());
setListAdapter(adapter);