i tried to update recyclerView after adding child to specific parent. for some reason it doesn't updated and I can't figure out the reason.
this is my database schema
here is two methods "retrieveChildrenFromDB" for retrieve specific parent's child's
public void retrieveChildrenFromDB() {
accountRef.orderByChild("id").equalTo(parentId).addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//loop through accounts to find the parent with that id
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
//loop through parent children to add them to adapter ArrayList
for (DataSnapshot userchildren:
userSnapshot.child("children").getChildren()) {
Child child = userchildren.getValue(Child.class);
children.add(child);
childsAdapter.notifyDataSetChanged();
childsAdapter.notifyItemRangeChanged(0, children.size());
recyclerView.invalidate();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
recyclerView.setAdapter(childsAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
and "addChildToParent" adding child to specific parent
private void addChildToParent() {
id=new Date().getTime()+"";
accountRef.orderByChild("id").equalTo(parentId).addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
if(CheckForfileds()) {
userSnapshot.getRef().child("children").push()
.setValue(new Child(parentId,id,childAge, "0",childName,
"0","0", "0"));
childsAdapter.notifyItemRangeChanged(0, children.size());
recyclerView.invalidate();
childsAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
question from:
https://stackoverflow.com/questions/65895319/why-recyclerview-not-updated-after-pushing-child-to-specific-parent 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…