Pass multiple values in an asynctask and receive corresponding results

In my Android app I am trying to pass in an asynctask during the splash screen multiple values and as a result it will return as many results as the values.

myasynctask task=new myasynctask()
task.execute("value1","value2",etc...)

and return for using in other activities result1, result2, etc.

How to do this?

Answers


You could encapsulate them in an DataTransferObject.

public class AsyncTaskDTO{
// implemnt your values
private int foo;
private boolean bar;
// getter and setters.
}

You could also pass a Map, an array or a List.

as ex:
AsyncTaskDto dto = new AsyncTaskDto();
dto.setFoo(2);
task.execute(dto);


List<String> values = new LinkedList<String>();
values.add("value1");
task.execute(values);

When you execute the task as in your code, you will get the values in params[0], parmas[1] etc., to return the same amount of results to onPostExecute I'd suggest to use an array or a list.

private class DownloadFilesTask extends AsyncTask<URL, Integer, ArrayList<String>> {
    protected Long doInBackground(URL... urls) {
        ArrayList<String> result = new ArrayList<String>();
        for (int i = 0; i < params.length; i++) {
            value = params[i];
            //do stuff
            result.add(whatever);
        }
        return result;
    }

    protected void onPostExecute(ArrayList<String> result) {
        //do stuff...
    }
}

from https://developer.android.com/reference/android/os/AsyncTask.html


public interface myInterface{
        public abstract void taskComplete(value1,value2... etc);
    }

Need Your Help

Self join vs group by when counting duplicates

sql oracle group-by duplicates self-join

I'm trying to count duplicates based on a column of a table in an Oracle Database. This query using group by:

Initialize an ArrayList of Class from An ArrayList of Names

java arraylist

I am trying to make a Student App. The user gives the number of students and then he will add the List of names of his students that will be stored in an ArrayList&lt;String&gt;.