0

I have this asyncTask:

public class CreateZipFile extends AsyncTask<ArrayList<String>, Integer, File> {

    Context context;

    public CreateZipFile(Context context){
        this.context = context;
    }

    protected File doInBackground(ArrayList<String>... files) {
        for(String file : files){
        //DO SMTH
        }
        return null;
    }

    public void onProgressUpdate(Integer... progress) {

    }

    public void onPostExecute() {

    }
}

however in my foreach loop I get error saying required ArrayList found String. Is it possible that asynctask converts my arraylist to String?

1

3 Answers 3

3

you don't need AsyncTask<ArrayList<String>, unless you want to pass an array of ArrayList. The ... operator, is called varargs, and it can be accessed like an array. E.g. if you call

 new CreateZipFile().execute("a", "b");

then, in

protected File doInBackground(String... files) {

files[0] contains a and files[1] contains b. If you still want to pass the ArrayList, then you have to change your code like follows:

 for (ArrayList<String> l : files) {
       for(String file : l){
           //DO SMTH
       }
   }
Sign up to request clarification or add additional context in comments.

Comments

2

Try to change protected File doInBackground(ArrayList<String>... files) { by this way:

protected File doInBackground(ArrayList<String>... files) {
        ArrayList<String> passedFiles = files[0]; //get passed arraylist

        for(String file : passedFiles){
        //DO SMTH
        }
        return null;
    }

Comments

0

You have to do something like this

    Items[] items = new Items[SIZE]; 
    items[0]=ITEM1;
    items[1]=ITEM2;
    items[2]=ITEM3;
    .
    .
    .

    new InsertItemsAsync().execute(items);   

private static class InsertItemsAsync extends AsyncTask<Items,Void,Void>{
        @Override
        protected Void doInBackground(Items... items) {
          // perform your operations here
            return null;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.