How can you pass multiple primitive parameters to AsyncTask?

后端 未结 6 672
情歌与酒
情歌与酒 2020-11-27 10:36

There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as pa

6条回答
  •  [愿得一人]
    2020-11-27 10:57

    The built in execute method accepts an array of Params, but they all must be of the defined type.. so if you simply set the PARAM type to OBJECT, then you can pass in whatever you like as long as they are children of objects....

    private class MyTask extends AsyncTask {
    

    Then in your doInBackGround, you simply cast each param in order back to what you need it to be:

     @Override
     protected void doInBackground(Object... params) {
         Context t = (Context)params[0];
         String a = (String) params[1];
         List list = (List)params[2];
         .
         .
         .
    

    And your execute is simply:

     new MyTask().execute(context,somestring,list_of_points);
    

    Not as good form as wrapping it in your own wrapper class, or a bundle, or hash or something, because you are order dependent on both sides, but it will work. Of course you could just make your array a param of HashMap(,) and you basically are custom implementing a bundle at that point, but it will work.

提交回复
热议问题