本文介绍了火力地堡检索子机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上访问火力地堡。我想要从树上一个孩子。我的code为(更新)

  f.child(德国)。子女(用户名)。子女(阿兰·图灵)。addChildEventListener(新ChildEventListener(){
        //获得新的职位,因为他们被添加到火力地堡
        @覆盖
        公共无效onChildAdded(DataSnapshot快照,字符串previousChildKey){
            地图<字符串,对象> newPost =(地图<字符串,对象>)snapshot.getValue();
            的System.out.println(作者:+ newPost.get(信息));
            disp_msg =(TextView中)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get(信息)的toString());
           //的System.out.println(标题:+ newPost.get(标题));
        }
 

在我的火力点我有

 语言
  德国
    用户名
      阿兰·图灵
        信息
          -Jf6ShYy7niHrqg_x4Tc:汤姆
          -Jf9v0xHAxINUUANrORU:非常完美
 

当我跑了code以上,我得到:

 作者:{
  用户名= {
    阿兰·图灵= {
      消息= {
        -Jf6ShYy7niHrqg_x4Tc =嗵,
        -Jf9v0xHAxINUUANrORU =完美!
      }
    }
  }
}
 

不过,我想只有信息,而不是整个火力地堡数据库。

因此​​,我怎么得到的只是指定的子?

这只是工作,但迄今为止它返回整个火力点:

  f.addChildEventListener(新ChildEventListener(){
        //获得新的职位,因为他们被添加到火力地堡
        @覆盖
        公共无效onChildAdded(DataSnapshot快照,字符串previousChildKey){
            地图<字符串,对象> newPost =(地图<字符串,对象>)snapshot.getValue();
            的System.out.println(作者:+ newPost.get(德国));
            disp_msg =(TextView中)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get(德国)的toString());
           //的System.out.println(标题:+ newPost.get(标题));
        }
 

我保存它,如下所示:

 火力地堡usersRef = f.child(语言);
。。。usersRef .child(德国)子(用户名)子(阿兰·图灵)子(信息)推()的setValue(writeMsgField.getText()的toString());
 

解决方案

您可以针对子节点直接,通过构建一个火力地堡参考关闭根:

  f.child(德国)
 .child(用户名)
 .child(阿兰·图灵)
 .child(消息)
 .addChildEventListener ...
 

或略偏conciser:

  f.child(德国/用户名/阿兰·图灵/信息)。addChildEventListener ....
 

您真的应该遵循火力地堡的Andr​​oid指南,因为数据结构覆盖相当不错的的话题数据是如何构成的火力地堡。花一些时间在该指南现在将节省您大量的时间(和许多问题)之后。

I am accessing Firebase on Android. I want to retrieve a child from the tree. My code is (updated)

         f.child("Germany").child("Username").child("Alan Turing").addChildEventListener(new ChildEventListener() {
        // Retrieve new posts as they are added to Firebase
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
            System.out.println("Author: " + newPost.get("Message"));
            disp_msg = (TextView)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get("Message").toString());
           // System.out.println("Title: " + newPost.get("title"));
        }

In my firebase I have

Language
  Germany
    Username
      Alan Turing
        Message
          -Jf6ShYy7niHrqg_x4Tc: "tom"
          -Jf9v0xHAxINUUANrORU: "perfect"

When I ran the code above I get:

Author: {
  Username={
    Alan Turing={
      Message={
        -Jf6ShYy7niHrqg_x4Tc=tom, 
        -Jf9v0xHAxINUUANrORU=perfect!
      }
    }
  }
}

But I want only the messages, not the entire Firebase database.

So how do I get just the specified child?

only this is working so far but it returns whole firebase:

         f.addChildEventListener(new ChildEventListener() {
        // Retrieve new posts as they are added to Firebase
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
            System.out.println("Author: " + newPost.get("Germany"));
            disp_msg = (TextView)findViewById(R.id.display_msg);
            disp_msg.setText(newPost.get("Germany").toString());
           // System.out.println("Title: " + newPost.get("title"));
        }

I stored it as follows:

Firebase usersRef = f.child("Language");
usersRef .child("Germany").child("Username").child("Alan Turing").child("Message").push().setValue(writeMsgField.getText().toString());
解决方案

You can address child nodes directly , by constructing a Firebase reference off the root:

f.child("Germany")
 .child("Username")
 .child("Alan Turing")
 .child("Message")
 .addChildEventListener... 

or a bit more conciser:

f.child("Germany/Username/Alan Turing/Message").addChildEventListener.... 

You really should follow the Firebase Android guide, because the data structure is covered quite well in the topic on how data is structured in Firebase. Spending some time on that guide now will save you much time (and many questions) later.

这篇关于火力地堡检索子机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:34