2

I have read through nearly every post on here about using publishProgress to set a new message on a progress dialog and I simply cannot get it to work.

Code:

public class LoginToApi extends AsyncTask<String, TaskProgress, String> {

        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setCancelable(false);
            pDialog.setMessage("Logging you in...");
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {

            HashMap<String, String> loginMap = new HashMap<String, String>();

            loginMap.put( "username", un.getText().toString() );
            loginMap.put( "password", pw.getText().toString() );
            loginMap.put( "session_id", "zOhAVPADzsGk0MZiMdXUzYLErP44pVZGWdHU7yD_Paek169umn");

            String loginXmlString = xmlCon.createXML("login", loginMap);
            String playlistDetails = xmlParse.makeHttprequest(url_login, loginXmlString);

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/adgen");
            File loginFile = new File(path, "login.xml");

            publishProgress(new TaskProgress(30, "A new update"));

            try {

                if(saveStringToFile(path, loginFile, playlistDetails)) {
                    Log.i("main", "login xml Saved");

                    // Read play.xml to array
                    //xmlParse.parse(file);

                    // Get playlist using playlist ID
                    HashMap<String, String> playlistMap = new HashMap<String, String>();

                    playlistMap.put("id", "158");

                    String playlistDetailsString = xmlCon.createXML("playlist", playlistMap);
                    String playlist = xmlParse.makeHttprequest(url_get_playlist, playlistDetailsString);

                    File playlistFile = new File(path, "playlist.xml");

                    try {
                        if(saveStringToFile(path, playlistFile, playlist)) {
                            Log.i("main", "playlist xml Saved");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                //Intent i = new Intent(getApplicationContext(), WebViewActivity.class);

                //startActivity(i);

                //finish();

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;

        }

        public void onProgressUpdate(TaskProgress progress) {
            pDialog.setProgress(progress.percentage);
            pDialog.setMessage(progress.message);
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }

    }

    private static class TaskProgress {
        final int percentage;
        final String message;

        TaskProgress(int percentage, String message) {
            this.percentage = percentage;
            this.message = message;
        }
    }

I get zero errors or warnings, my code runs fine, with my files being saved and eventually the progress dialog being removed after the Async task is complete. but the dialog message simply wont update when I use publishProgress.

I used this post as a guide, but as you can see, it looks like the OP had trouble even after a solution was given.

What am I doing wrong?

Thanks1

Community
  • 1
  • 1
Mr Pablo
  • 4,109
  • 8
  • 51
  • 104

1 Answers1

1

Change your method to:

public void onProgressUpdate(TaskProgress... progress) {
   pDialog.setProgress(progress[0].percentage);
   pDialog.setMessage(progress[0].message);
}

The ... is important.

The method signature it expects is

onProgressUpdate(TaskProgress... progress)

NOT

onProgressUpdate(TaskProgress progress)

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • What does the "..." mean? sorry, like I said, I'm new to Android/Java and its hit and miss finding useful information on the web (that is easy to understand and in-depth) – Mr Pablo Jun 05 '13 at 16:04
  • 2
    Also, to avoid these kind of problem due to incorrect method signature, it's better to put an '@Override' annotation before the method declaration. – TactMayers Jun 05 '13 at 16:10