简体   繁体   中英

Android Asynctask displaying circle behind the progress dialog

I would like to completely hide the Progress circle shown during the execution of an AsyncTask (code below) - whilst showing a progress dialog. The problem is that the circle can be seen through the transparent progress dialog. Can someone help? I would prefer not to change the transparency of the Progress dialog...

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

I also saw some strange dialog behind the normal one with your code. Try making a separate AsyncTask class instead.

Something like:

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

And then call it with:

MyAsyncTask asyncTask = new MyAsyncTask();
                 asyncTask .execute();

I have found the cause of this problem: its the fact that the ListFragment's ListAdapter had not yet been set. The solution is to set the ListAdapter to an empty list, which tells the ListFragment that it has it's results and that other processing is not happening.

The problem was resolved by adding the following 2 lines to the onCreate method

ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(getActivity(), R.layout.productlist_item, new ArrayList<Product>());
setListAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM