简体   繁体   中英

Progress Dialog not showing during a asynctask call

Ok so I am trying to get a progress dialog to show when data is being downloaded from the internet and being applied to the UI. My asynctask is working fine and it runs though all the steps, but it never shows the dialog ever. I don't know what I have done wrong here and I pretty much hit the wall. I even tried to put the async task into the new runnable thread and run it like that, that also did not show the dialog. I am calling the async task like this

    new runningMan().execute();

Here is the code that I am trying to run.

private class runningMan extends AsyncTask<Void, Void, Integer>
{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        Log.d("Runningman: ", "Started running");
        //this method will be running on UI thread
        progress = ProgressDialog.show(PromotionMain.this, "Loading", "PleaseWait", true);

    }
    @Override
    protected Integer doInBackground(Void... params) {
        //parse the JSON string
        JSONParser jp = new JSONParser();
        try {
            Log.d(username , password);
            jp.parsesData(promos, myJson, pictureArray, pathArray, labelArray);
            Log.d("Runningman: ", "Finished parsing");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }
    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        ArrayList<ListItem> listData = getListData();
        fillListView(listData);
        Log.d("Runningman: ", "Finished runing");
        //this method will be running on UI thread
        progress.dismiss();
    }

}

Thank you for any help with this.

Call "super.onPreExecute();" and "super.onPostExecute(result);" after your code for progress dialog. Or better, get rid of them (if you don't have reasons for calling them).

Use the following code:

private class runningMan extends AsyncTask<Void, Void, Integer>
{

    @Override
    protected void onPreExecute() {

        Log.d("Runningman: ", "Started running");
        //this method will be running on UI thread
        progress = ProgressDialog.show(PromotionMain.this, "Loading", "PleaseWait", true);
        super.onPreExecute();

    }
    @Override
    protected Integer doInBackground(Void... params) {
        //parse the JSON string
        JSONParser jp = new JSONParser();
        try {
            Log.d(username , password);
            jp.parsesData(promos, myJson, pictureArray, pathArray, labelArray);
            Log.d("Runningman: ", "Finished parsing");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }
    @Override
    protected void onPostExecute(Integer result) {
        ArrayList<ListItem> listData = getListData();
        fillListView(listData);
        Log.d("Runningman: ", "Finished runing");
        //this method will be running on UI thread
        progress.dismiss();
        super.onPostExecute(result);
    }

}

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