How can you pass multiple primitive parameters to AsyncTask?

后端 未结 6 676
情歌与酒
情歌与酒 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:58

    Another way: You just need add MyTask constructor in your MyTask class:

    private class MyTask extends AsyncTask {
        int foo;
        long bar;
        double arple;
    
        MyTask(int foo, long bar, double arple) { 
             // list all the parameters like in normal class define
            this.foo = foo;
            this.bar = bar;
            this.arple = arple;
        }
        ......   // Here is doInBackground etc. as you did before
    }
    

    Then call

    new MyTask(int foo, long bar, double arple).execute();
    

    A second way like David Wasser's Answer.

提交回复
热议问题