简体   繁体   中英

Recyclerview item shows two time after deletion

I'm having a little issue here with my android app. I am trying to delete an item from the recyclerview and at the same from the firebase database. Everything is working fine except that I notice that every time after I delete an item it shows another item two times. For example lets say I have two items, Item_A and Item_B. If I delete Item_A then Item_B shows two times. How can I prevent this from happening

 //Code 
    
     FirebaseDatabase.getInstance().getReference()
                                            .child("items").child(uid).child(users.get(position).id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            Toast.makeText(mContext, "Deleted", Toast.LENGTH_SHORT).show();;
    =
                                        }
                                    });




  databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                progressBar.setVisibility(View.GONE);

                if(dataSnapshot.exists()){

                      for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {

                          UserInformation2 upload=postsnapshot.getValue(UserInformation2.class);


                          myUploads.add(upload);
                          aAdapter = new ImageAdapter2(ProfileActivity.this, myUploads);
                          recyclerView.setAdapter(aAdapter);
                          aAdapter.notifyDataSetChanged();
                          recyclerView.invalidate();


                      }

                      linearLayout.setVisibility(View.GONE);







                    aAdapter.notifyDataSetChanged();

                }else{

                    //txt1.setVisibility(View.VISIBLE);
                    linearLayout.setVisibility(View.VISIBLE);


                }

You're attaching a listener to the database with:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        ...

When you run this code, Firebase will immediately load the current data at databaseReference and call your onDataChange with a snapshot of that data. Then it continues to observe the database, and if there's a chance at databaseReference it again calls onDataChange with the updated snapshot.

So each time onDataChange is called, it gets a full snapshot of the data at databaseReference . For this reason, you'll want to clear the current data in myUploads before adding the data from the latest snapshot.

So add myUploads.clear(); as the first line inside onDataChange .

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