How To Retrieve Parent Key In Firebase Android
I want to retrieve houfxWvbwyVmbHX60IGjpNkZR9w2 key , How to do? Here is the code for the same mQuery = mfollowing.orderByChild('following').equalTo(user_id); mQuery.addListenerFor
Solution 1:
Assuming you have a DataSnapshot
that points to the children rhZ4...wt2
, you can call snapsthot.getRef().toString()
which will return you something like https://xxx.firebaseio.com/.../houfxWvbwyVmbHX60IGjpNkZR9w2/rhZ4...wt2
. From there you can extract the parent key.
From your example, after you query the database using mQuery
, inside onDataChange
you have a DataSnapshot
.
So calling dataSnapshot.getRef().toString()
will give you https://xxx.firebaseio.com/.../houfxWvbwyVmbHX60IGjpNkZR9w2/rhZ4...wt2/following
.
Code to extract the parent key (put it inside your 1st onDataChange
):
String reference = dataSnapshot.getRef().toString();
String[] tokens = reference.split("/");
String parentKey = tokens[tokens.length - 3];
This parentKey
should be your houfxWvbwyVmbHX60IGjpNkZR9w2
.
Post a Comment for "How To Retrieve Parent Key In Firebase Android"