How to maintain uploading task in adapter when notifyItemRemoved or notifyDatasetChabged?

人走茶凉 提交于 2020-07-23 06:30:07

问题


Here i am implementing ,

  • user pick items
  • add in adapter
  • adapter current position directly upload to firebase without any click listener

For this code is, In Activity.

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null)
        {
            switch (requestCode)
            {
                case PICK_IMAGE:
                    ImageUri = data.getData();
                    Log.i("wneuiewnewu",ImageUri.getPath());
                    UriListFromGallery.add(ImageUri);
                    postImageAdapter.notifyItemInserted(UriListFromGallery.size()-1);
                    break;
            }
        }
    }

In Adapter class @Override

 public void onBindViewHolder(@NonNull final ImageViewHolder imageViewHolder, final int i) {
 final StorageReference filepath=PostReference.child("image").child(currentUserID).child(currentUserID +String.valueOf(tsLong)+FORMAT_IMAGE);
        final UploadTask task=filepath.putFile(imagelist.get(i));

task.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                iImageViewHolder.progressBar.setProgress((int) progress);
imageViewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                task.cancel();
                imagelist.remove(i);
                notifyitemremoved(i);
            }
        });
            }
        });
}

working is well, Let the problem explain by example, Suppose imagelist has 5 item.

  • 0 1 2 3 4 and if 3rd element (index 2) removed..notifyitemremoved(2) is called,so the old element (index 3 & index 4) is now become a (index 2 & index 3). By changing this index onBindViewHolder swap new layout and start one more uploading task. so how to prevent this one more uploading and keep continuing old uploading task ?

来源:https://stackoverflow.com/questions/61452776/how-to-maintain-uploading-task-in-adapter-when-notifyitemremoved-or-notifydatase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!