You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java从Firebase获取新增子节点的父键及指定Key?

How to Retrieve a Specific Key and Get Parent Key of Newly Added Child Nodes in Firebase Realtime Database (Java)

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 the DatabaseReference of 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:28:14