Florian Walther
Florian Walther

Reputation: 6971

Firebase + RecyclerView: notifyItemRemoved not working properly

I am able to delete my entries from the Firebase storage and database, but when I call notifyItemRemoved, it's not working properly. It isn't deleting the entry and I think this is because its interferring with the ValueEventListener which is also triggered on the update. When I leave the activity and reopen it, the entries are (correctly) gone.

Can anyone suggest me what I have to change in my activity for this to work properly?

public class ImagesActivity extends AppCompatActivity implements ImageAdapter.OnItemClickListener {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;

private ProgressBar mProgressCircle;

private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_images);

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {               
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {  
                Upload upload = postSnapshot.getValue(Upload.class);        
                upload.setKey(postSnapshot.getKey());
                mUploads.add(upload);
            }

            mAdapter = new ImageAdapter(ImagesActivity.this, mUploads);

            mRecyclerView.setAdapter(mAdapter);

            mAdapter.setOnItemClickListener(ImagesActivity.this);

            mProgressCircle.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ImagesActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
            mProgressCircle.setVisibility(View.INVISIBLE);
        }
    });
}

@Override
public void onDeleteClick(int position) {
    Upload selectedItem = mUploads.get(position);
    final String selectedKey = selectedItem.getKey();

    StorageReference imageRef = FirebaseStorage.getInstance().getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            mDatabaseRef.child(selectedKey).removeValue();
        }
    });

    mAdapter.notifyItemRemoved(position);
}
}

Upvotes: 1

Views: 412

Answers (1)

Yupi
Yupi

Reputation: 4470

Because your List is populated with data and no matter if you remove data from database it is still inside the List. So you need first to remove data from List and then notify adapter that data is removed. For example:

mUploads.remove(position); mAdapter.notifyItemRemoved(position);

To play animation after deleting try to use:

notifyItemRangeChanged(position, getItemCount());

Consider removing the adapter class from onDataChange to prevent recreating the adapter again and again. Also in future when working with the Firebase database and RecyclerView consider using: https://github.com/firebase/FirebaseUI-Android

Upvotes: 1

Related Questions