Updating Firebase Database With Nodes Created With Push
Solution 1:
I have finally got this to work after days of trying to figure it out.
I added a Query with orderByChild
method to filter out a big chunk of data at the server side itself (thanks to this answer for the idea.) The remaining data which I got as child
of data type DataSnapshot
had all the necessary information that I needed. Here is the query
Query query = db.orderByChild("r_id").equalTo(f_id);
query.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String child_s_id = (String) child.child("s_id").getValue();
String child_status = (String) child.child("status").getValue();
if (child_s_id.equals(id)) {
Log.e("Got value", f_id + " - x -" + id + " " + child_status);
}
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
But again I could not meet my requirements, because I actually needed to modify one of the value in the child
and I could not use any methods like setValue
to change the value of child
.
Again, it took me a long while (what an idiot) to figure out that I had to convert the child
(of data type DataSnapShot
) to a reference for setValue
to work. And it worked
child.getRef().child("status").setValue("accepted");
Here is the completed code. Hope it helps someone along the way
Query query = db.orderByChild("r_id").equalTo(f_id);
query.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String child_s_id = (String) child.child("s_id").getValue();
String child_status = (String) child.child("status").getValue();
if (child_s_id.equals(id))
child.getRef().child("status").setValue("accepted");
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
}
});
Post a Comment for "Updating Firebase Database With Nodes Created With Push"