如何将多个原始参数传递给 AsyncTask? [英] How can you pass multiple primitive parameters to AsyncTask?

查看:23
本文介绍了如何将多个原始参数传递给 AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有相关问题,比如如何将 2 个参数传递给 AsyncTask 类?,但是我遇到了将多个原语作为参数传递给 AsyncTask 的尝试徒劳无功的困难,所以我想分享我的发现.现有的问题和答案中没有体现这种微妙之处,因此我想帮助任何遇到与我相同问题的人,并为他们省去痛苦.

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 parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain.

问题是这样的:我有多个 原始 参数(例如两个 long),我想将它们传递给要在后台执行的 AsyncTask——怎么做?(我的答案……在为此挣扎了一段时间之后……可以在下面找到.)

The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to an AsyncTask to be executed in the background--how can it be done? (My answer...after struggling with this for awhile...can be found below.)

推荐答案

(严格来说)不可能将多个原语传递给 AsyncTask.例如,如果要执行myTask.execute(long1, long2) 并尝试设置私有类myTask extends AsyncTask 与相应的方法:

It is (strictly-speaking) NOT possible to pass multiple primitives to AsyncTask. For example, if you want to perform myTask.execute(long1, long2) and try to set up private class myTask extends AsyncTask<long, Void, Void> with the corresponding method:

@Override
protected LocationItemizedOverlay doInBackground(long... params) {...}

您的 IDE 可能会抱怨需要覆盖超类型方法.请注意,您正在使用所谓的 Varargs doInBackground 的方法签名,其中 (long... params) 就像在说我接受可变数量的 long,存储为名为 params 的数组.我不"不完全理解导致编译器/IDE 投诉的原因,但我认为这与泛型类 Params 的定义方式有关.

your IDE will likely complain about needing to override a supertype method. Note that you are using the so-called Varargs method signature for doInBackground, where (long... params) is like saying "I accept a variable number of longs, stored as an array called params. I don't completely understand what causes a compiler/IDE complaint to be raised, but I think it has to do with how the generic class Params is defined.

在任何情况下,只要您正确地将基元转换为它们各自的非基元包装器(例如 int => Integer、long => Long 等),就可以毫无问题地实现您想要的目标.实际上,您不需要将原语显式转换为非原语.Java 似乎可以为您处理.您只需要按如下方式设置您的 ASyncTask(以 long 为例):

In any case, it is possible to achieve what you want with no problem, provided you correctly cast your primitives to their respective non-primitive wrappers (e.g. int => Integer, long => Long, etc.). Actually, you don't need to explicitly cast your primitives to non-primitives. Java seems to handle that for you. You just need to set up your ASyncTask as follows (for the example of longs):

private class MyTask extends AsyncTask<Long, Void, Void> {

    @Override
    protected void doInBackground(Long... params) {
        // Do stuff with params, for example:
        long myFirstParam = params[0]
    }
    ...
}

然后您可以按原先的意图使用该类,例如:

You can then use this class as you originally intended, e.g.:

MyTask myTask = new MyTask();
myTask.execute(long1, long2);

或者对于您想要的任意数量的基元,只要它们是相同的类型.如果需要传递多种类型的原语,也可以这样做,但是需要将上面的修改为:

Or for any number of primitives that you would like, PROVIDED THEY ARE OF THE SAME TYPE. If you need to pass multiple types of primitives, this can also be done, but you will need to modify the above to:

private class MyTask extends AsyncTask<Object, Void, Void> {

    @Override
    protected void doInBackground(Object... params) {
        // Do stuff with params, for example:
        long myLongParam = (Long) params[0];
        int myIntParam = (Integer) params[1];

    }
    ...
}

这更灵活,但它需要明确地将参数转换为它们各自的类型.如果不需要这种灵活性(即单一数据类型),我建议坚持使用第一个选项,因为它稍微更具可读性.

This is more flexible, but it requires explicitly casting the parameters to their respective types. If this flexibility is not needed (i.e. a single data type), I recommend sticking to the first option, as it's slightly more readable.

这篇关于如何将多个原始参数传递给 AsyncTask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆