Issue
Read couple of question here on stack overflow but couldn't find the working solution.
My scenario => In onActivityResult method, receiving a list which is send to Async Task for some background work. After performing some work, it sends back a list say output. After receiving output back in Async callback, logged theoutput list and it works as expected.
Now from here, i want to put it in an Arraylist say addProductModelListTemp so i can add further objects. But on adding other objects in background as illustrated in above scenario, addProductModelListTemp gets overridden all of its indexes with the last output list received in Async callback. Below is the code how i am updating addProductModelListTemp. currentRowIdToInsertImages is the index to insert object in addProductModelListTemp which is working fine(Not included here for simplicity)
new CompressFileAsyncTask(new AsyncResponse() {
@Override
public void processFinish(List<Uri> output) {
for (int i = 0; i < output.size(); i++) {
Timber.e("Received from Async Class contains => " + output.get(i));
}
AddProductModel tempProductModel = new AddProductModel("", "", "", dummyList);// Dummy list is an empty list you can assume ... Tried setting it directly as well with the **output**
addProductModelListTemp.add(tempProductModel);
addProductModelListTemp.get(currentRowIdToInsertImages).setImagePathList(output);
for (int i = 0; i < addProductModelListTemp.size(); i++) {
Timber.e("addProductModelListTemp index " + i);
for (int k = 0; k < addProductModelListTemp.get(i).getImagePathList().size(); k++) {
Timber.e("addProductModelListTemp.get(i).getImagePathList() index " + k + " contains " + addProductModelListTemp.get(i).getImagePathList().get(k));
}
}
}
}).execute(imageURIList); // imageURIList is the list received in onActivityResult method
Below is the model class through which i am creating & updating objects.
public AddProductModel(String size, String color, String quantity, List<Uri> imagePath) {
this.size = size;
this.color = color;
this.quantity = quantity;
this.imagePath = imagePath;
}
public void setImagePathList(List<Uri> imagePath) {
if (this.imagePath.isEmpty()) {
Timber.e("Image Path List not exists, adding");
this.imagePath = imagePath;
} else {
Timber.e("Image Path List exists, appending");
this.imagePath.addAll(this.imagePath.size(), imagePath);
}
} // Getter setter not attached for clarity}
The AsyncTask class is
class CompressFileAsyncTask extends AsyncTask<List<Uri>, Void, List<Uri>> {
public AsyncResponse asyncResponse = null;
public CompressFileAsyncTask(AsyncResponse asyncResponse) {
this.asyncResponse = asyncResponse;
}
@Override
protected List<Uri> doInBackground(List<Uri>... imageUriList) {
List<Uri> fileList = imageUriList[0];
for (int i = 0; i < fileList.size(); i++) {
filesAdded.add(Uri.parse(compressImage(String.valueOf(fileList.get(i)))));
}
return filesAdded;
}
@Override
protected void onPostExecute(List<Uri> uriArrayList) {
asyncResponse.processFinish(uriArrayList);
}
}
And the interface itself
public interface AsyncResponse {
public void processFinish(List<Uri> output);
}
Solution
The issue is that you are modifying the same list object and storing it in each index of addProductModelListTemp
. You need to make filesAdded
a local variable.
@Override
protected List<Uri> doInBackground(List<Uri>... imageUriList) {
List<Uri> fileList = imageUriList[0];
List<Uri> filesAdded = new ArrayList<>();
for (int i = 0; i < fileList.size(); i++) {
filesAdded.add(Uri.parse(compressImage(String.valueOf(fileList.get(i)))));
}
return filesAdded;
}
Answered By - Rishabh Jain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.