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

Firebase开发:获取含子节点的子节点名称及TextView显示child1.1/1.2

Hey there! Let's work through your Firebase and TextView problem together. Since you can't share a screenshot, I'll start with a sample Firebase JSON structure that matches your description of parent nodes containing sub-nodes, then show you exactly how to get the data you need.


Step 1: Sample Firebase Structure (Matching Your Description)

Let’s assume your Firebase Realtime Database looks something like this (adjust the node names to match your actual structure):

{
  "mainParentNode": {
    "child1": {
      "1.1": "This is the content for child 1.1",
      "1.2": "This is the content for child 1.2"
    },
    "child2": {
      "2.1": "Content for another child's sub-node"
    }
  }
}

Step 2: Get Names of Child Nodes That Have Sub-Nodes

To fetch the names of parent-level children (like child1, child2) that contain their own sub-nodes, you can use getChildren() to iterate through the parent node, then check if each child has sub-nodes with hasChildren():

// Initialize reference to your main parent node
DatabaseReference mainRef = FirebaseDatabase.getInstance().getReference("mainParentNode");

mainRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
            // Check if the current child has sub-nodes
            if (childSnapshot.hasChildren()) {
                String childNodeName = childSnapshot.getKey(); // Gets "child1", "child2", etc.
                Log.d("FirebaseLogs", "Child with sub-nodes: " + childNodeName);
                // You can store these names in a list or use them immediately here
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.e("FirebaseError", "Failed to fetch child names", databaseError.toException());
    }
});

Step 3: Fetch & Display Content from child1.1 and 1.2 in TextView

There are two simple ways to do this—either target each sub-node directly, or iterate through all sub-nodes under child1:

Option 1: Target Specific Sub-Nodes Directly

If you know the exact names of the sub-nodes you want (1.1 and 1.2), you can point directly to their paths:

// Fetch content for child1.1
DatabaseReference child1_1Ref = FirebaseDatabase.getInstance().getReference("mainParentNode/child1/1.1");
child1_1Ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String content1_1 = dataSnapshot.getValue(String.class);
        if (content1_1 != null) {
            TextView tv1_1 = findViewById(R.id.your_textview_id_1);
            tv1_1.setText(content1_1);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.e("FirebaseError", "Failed to fetch 1.1 content", databaseError.toException());
    }
});

// Fetch content for child1.2 (repeat the pattern)
DatabaseReference child1_2Ref = FirebaseDatabase.getInstance().getReference("mainParentNode/child1/1.2");
child1_2Ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String content1_2 = dataSnapshot.getValue(String.class);
        if (content1_2 != null) {
            TextView tv1_2 = findViewById(R.id.your_textview_id_2);
            tv1_2.setText(content1_2);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.e("FirebaseError", "Failed to fetch 1.2 content", databaseError.toException());
    }
});

Option 2: Iterate Through All Sub-Nodes Under child1

If you want to display all sub-node content under child1 (in case you add more later), use a loop:

DatabaseReference child1Ref = FirebaseDatabase.getInstance().getReference("mainParentNode/child1");
child1Ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        TextView combinedTextView = findViewById(R.id.your_combined_textview_id);
        StringBuilder contentBuilder = new StringBuilder();

        for (DataSnapshot subChildSnapshot : dataSnapshot.getChildren()) {
            String subNodeName = subChildSnapshot.getKey(); // Gets "1.1", "1.2"
            String subNodeContent = subChildSnapshot.getValue(String.class);
            contentBuilder.append(subNodeName).append(": ").append(subNodeContent).append("\n");
        }

        combinedTextView.setText(contentBuilder.toString());
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.e("FirebaseError", "Failed to fetch sub-node content", databaseError.toException());
    }
});

Quick Notes to Avoid Issues

  • Double-check your Firebase database rules to ensure you have read access (for development, you can temporarily set "read": true, "write": true—just remember to lock this down for production).
  • Make sure you’ve properly configured Firebase in your project (added the google-services.json file and included the necessary dependencies in your build.gradle files).
  • Adjust all node names (mainParentNode, child1, etc.) to match your actual Firebase structure.

内容的提问来源于stack exchange,提问作者Aashna Choudhary

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:10:48