Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is (strictly-speaking) NOT possible to pass multiple primitives to AsyncTask. For example, if you want to perform <code>myTask.execute(long1, long2)</code> and try to set up <code>private class myTask extends AsyncTask&lt;long, Void, Void&gt;</code> with the corresponding method:</p> <pre><code>@Override protected LocationItemizedOverlay doInBackground(long... params) {...} </code></pre> <p>your IDE will likely complain about needing to override a supertype method. Note that you are using the so-called <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html" rel="noreferrer">Varargs</a> method signature for <code>doInBackground</code>, where <code>(long... params)</code> 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 <code>Params</code> is defined.</p> <p>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):</p> <pre><code>private class MyTask extends AsyncTask&lt;Long, Void, Void&gt; { @Override protected void doInBackground(Long... params) { // Do stuff with params, for example: long myFirstParam = params[0] } ... } </code></pre> <p>You can then use this class as you originally intended, e.g.:</p> <pre><code>MyTask myTask = new MyTask(); myTask.execute(long1, long2); </code></pre> <p>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:</p> <pre><code>private class MyTask extends AsyncTask&lt;Object, Void, Void&gt; { @Override protected void doInBackground(Object... params) { // Do stuff with params, for example: long myLongParam = (Long) params[0]; int myIntParam = (Integer) params[1]; } ... } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload