Reputation: 466
I have one question, I have a recycle view and inside it, I have some items that come from firebase database, when I press the button to delete the item, the item disappears from the dataset but the recycle view is displaying me the item that stands below the item that I just deleted and I don't understand why since I checked stack overflow on how to do it and I did the same thing but it's not working.
I will add 3 photos and the code to explain myself better.
Can anyone explain what's going on ?? I think it is a problem with the refresh of the recycle view but not sure 100%.
Code:
public class CartActivity extends AppCompatActivity implements OnRemoveItemClickListener{
private TextView total;
private ImageButton removeFromCartt;
private Button pay;
private RecyclerView mResultList2;
private DatabaseReference mUserDatabase;
int i=0;
int Totalprice=0;
private UsersAdapter adapter;
private ArrayList<Users_get> array_data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mUserDatabase = FirebaseDatabase.getInstance().getReference("Cart");
total = findViewById(R.id.TotalPrice);
removeFromCartt = findViewById(R.id.removeFromCart);
mResultList2 = findViewById(R.id.cartList);
pay = findViewById(R.id.pay);
mResultList2.setHasFixedSize(true);
mResultList2.setLayoutManager(new LinearLayoutManager(this));
// Attach a listener to read the data at our posts reference
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int flag=1;
for(DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Users_get post = childSnapshot.getValue(Users_get.class);
array_data.add(new Users_get(post.getName(),post.getSurname(),post.getPrice(),childSnapshot.getKey()));
getTotalPrice(post.getPrice(),flag);
}
setAdapter();
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
private void setAdapter() {
adapter = new UsersAdapter(array_data,CartActivity.this);
mResultList2.setAdapter(adapter);
}
public int getTotalPrice(Long price,int flag){
if (array_data != null) {
if(flag==1)
{
Totalprice+=price;
}
else
{
Totalprice-=price;
}
}
System.out.println(Totalprice);
return Totalprice;
}
@Override
public void onRemoveItemClicked(final int position) {
int flag=0;
FirebaseDatabase.getInstance().getReference("Cart").child(array_data.get(position).getKey()).removeValue();
getTotalPrice(array_data.get(position).getPrice(),flag);
array_data.remove(position);
adapter.notifyItemRemoved(position);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(adapter !=null){
adapter.removeListener();
}
}
}
Upvotes: 0
Views: 37
Reputation: 3394
Try this, When you are deleting item, firebase notify again and the same set of data gets appended to the same array_data list.
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (adapter == null) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Users_get post = childSnapshot.getValue(Users_get.class);
array_data.add(new Users_get(post.getName(), post.getSurname(), post.getPrice(), childSnapshot.getKey()));
getTotalPrice(post.getPrice());
}
setAdapter();
} else {
adapter.notifyDataSetChanged();
}
}
Upvotes: 1