简体   繁体   中英

how to update recyclerview when delete an item from adapter class

everyone, I am working with recycler view in android and I'm getting data from firebase. The problem is when I press the delete button data is deleted from firebase but in the application new list is added with the old list but I want to show only the new list which does not change the position of the recycler view only remove that item. I'm doing this all in the adapter class.

    final DatabaseReference referencePost = database.getReference().child("Posts").child(holder.postId);

  referencePost.setValue(null).addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (!task.isSuccessful()) {
                                                    Toast.makeText(holder.itemView.getContext(), "" + Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
                                                }
                                                dialog.dismiss();
                                            }
                                        });

After making sure that the deletion process from Firebase is complete, you can add this line:

.
.
//Successfully Deleted

notifyDataSetChanged ();

You can read about it more than this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM