如何用Java从Firebase获取新增子节点的父键及指定Key?
Hey there! Let's tackle your two Firebase Realtime Database needs: fetching data for a specific key, and retrieving the parent key of newly added child nodes. Here's how to fix your code and achieve both goals:
1. Fetch Data for Your Target Key
If the key 7753400075_Canada_twinpalms_||_twinpalms_20170824001545_F5BA7F33AD51CE283332001BEC409A46 sits under chatmessage/devicetoken, you can directly reference this path and use a single-value listener to pull its data:
// Directly reference your specific key DatabaseReference specificKeyRef = ref.child("chatmessage").child("devicetoken") .child("7753400075_Canada_twinpalms_||_twinpalms_20170824001545_F5BA7F33AD51CE283332001BEC409A46"); specificKeyRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { Object specificData = dataSnapshot.getValue(); System.out.println("Data for target key: " + specificData); } else { System.out.println("Target key does not exist in the database."); } } @Override public void onCancelled(DatabaseError databaseError) { System.out.println("Error fetching target key: " + databaseError.getMessage()); } });
2. Get Parent Key of Newly Added Child Nodes
Your existing ChildEventListener is on the right path, but you're missing the code to grab the parent key. Each DataSnapshot in onChildAdded represents the newly added child node—you can access its parent reference and extract the parent key using dataSnapshot.getRef().getParent().getKey().
Here's the corrected, complete listener code:
ref.child("chatmessage").child("devicetoken").addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { // Get the key of the newly added child String childKey = dataSnapshot.getKey(); System.out.println("Newly added child key: " + childKey); // Retrieve the parent key of this child (will be "devicetoken" in your structure) String parentKey = dataSnapshot.getRef().getParent().getKey(); System.out.println("Parent key of the new child: " + parentKey); // Optional: Fetch the data associated with the new child Object childData = dataSnapshot.getValue(); System.out.println("Data of new child: " + childData); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) { // Handle child data updates if needed } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { // Handle child deletion if needed } @Override public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { // Handle child position changes if needed } @Override public void onCancelled(DatabaseError databaseError) { System.out.println("Child listener error: " + databaseError.getMessage()); } });
Quick Explanations:
dataSnapshot.getRef()returns theDatabaseReferenceof the newly added child node.- Calling
.getParent()on that reference gives you the parent node's reference, and.getKey()pulls the parent's key value. - Remember to implement all required methods of
ChildEventListener—your original code was missing some, which would cause compilation issues.
内容的提问来源于stack exchange,提问作者Komal Dhiman

