Pages

Thursday, March 21, 2013

Async task multiple parameters


MULTIPLE PARAMTERES of different type


First, create your AsyncTask as normal, but within it, create a constructor:
    private class StartTask extends AsyncTask<Context, Void, Boolean> 
    {
        private ProgressDialog progress;
        private String strAction="";

        public StartTask(ProgressDialog progress, String Action)
        {
            this.progress = progress;
            this.strAction = Action;
        }
    }
Now, on your event or anything else, when you want to kick off the action you call your AsyncTask and pass as many parameters as you want.
    ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("Loading...");
    String strAction = "this_is_a_string";
    new StartTask(progress, strAction).execute(this);
You can see that calling "StartTask" and passing the constuctor parameters will now assign the variables within the StartTask class.
MULTIPLE PARAMETERS OF SAME TYPE
Read Params... params as Params[] params. You can send as many params as you want.

passing multiple doInBackground(String... params):
 task.execute(uri, username, password, etc...); 
getting: return Login.getResponseXML(params[0], params[1], params[2], etc

No comments:

Post a Comment