Reputation: 31
In my App I have a list of plants and the user can insert new ones, modify and delete them.
The user can longClick one of the items of the list to delete it but if someone else delete the same item from another device or me from database the List doesn't update like in the edit (the item stays in the list and you can also delete it again which makes no sense).
Here is my way to update the item when data changes:
PlantViewHolder(View itemView, Context context) {
super(itemView);
cardView = (CardView)itemView.findViewById(R.id.cardView);
plantCommonName = (TextView)itemView.findViewById(R.id.plantCommonName);
plantScientificName = (TextView)itemView.findViewById(R.id.plantScientificName);
plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
plantCategory = (TextView)itemView.findViewById(R.id.plantCategory);
plantDescription = (TextView)itemView.findViewById(R.id.plantDescription);
plantPrice = (TextView)itemView.findViewById(R.id.plantPrice);
plantStock = (TextView)itemView.findViewById(R.id.plantStock);
this.context = context;
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
database.getReference().child("plants").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dss: dataSnapshot.getChildren()) {
Plant p0 = dss.getValue(Plant.class);
editAt(p0);
}
}
@Override
public void onCancelled(DatabaseError error) {
Log.w("Failed to read value.", error.toException());
}
});
}
Here is the method:
private void editAt(Plant p0) {
for(int i = 0; i < plants.size(); i++) {
if(plants.get(i).getID() == p0.getID()) {
plants.get(i).setCommonName(p0.getCommonName());
plants.get(i).setScientificName(p0.getScientificName());
plants.get(i).setPrice(p0.getPrice());
plants.get(i).setStock(p0.getStock());
plants.get(i).setSeason(p0.getSeason());
plants.get(i).setDescription(p0.getDescription());
notifyItemRangeChanged(i, plants.size());
}
}
}
As you can see I just modify the specific item in the List but I have no idea how to differentiate or implement delete in the database listener.
How can I detect if an item is deleted from the database and remove it with ValueEventListener
?
Upvotes: 2
Views: 1689
Reputation: 31
I solved it by getting the snapshot from onChildRemoved
and checking in the List if this ID is in this snapshot.
database.getReference().child("plants").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Plant p0 = dataSnapshot.getValue(Plant.class);
editAt(p0);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Plant p0 = dataSnapshot.getValue(Plant.class);
for(int i = 0; i < plants.size(); i++) {
if(plants.get(i).getID() == p0.getID()) {
plants.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, plants.size());
}
}
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 1
Reputation: 138824
You don't need to use ValueEventListener
to remove a value. Just find your reference
and than use removeValue()
method. Assuming you have a node named Plants
please use this code:
yourReference.child("Plants").child(plantId).removeValue();
In which plantId
is the particulat plant you want to delete.
If you want to verify if an item exists, take your reference
, and than use on the SanpShop
the method exists()
.
Hope it helps.
Upvotes: 1