FireBase查询.数据快照 [英] FireBase Query. DataSnapshot

查看:76
本文介绍了FireBase查询.数据快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关从此DataSnapshot结果中获取消息"对象的帮助

I need help on getting the "message" Object from this DataSnapshot result

DataSnapshot { key = user-4, value = {-JvFuwKX7r7o0ThXc0x8={sender=unit_owner, message=fkfkgkgkgkggkgkgkgkgkgkgkgkgkgkgkgkglgkgkgkgkgkgkgkg, role=unit_owner, profile_image=http://54.169.41.20/assets/boy-862a2e9036b094973c04afa1c0365a45.png, name=Paul Bartbartbart}} }

我能够通过 getKey()获取键值,并且我也有 JvFuwKX7r7o0ThXc0x8 作为对象,这是FireBase自动生成的对象名称.而不是听对象本身.如何使用DataSnapshot成员方法浏览低谷,发送者"或消息"并获得其价值?

I am able to get the key value by getKey() and I have JvFuwKX7r7o0ThXc0x8 as an Object as well, this is an automatic FireBase generated name of the object. Instead of listening to the object itself. How can I navigate trough, "sender" or "message" and getting its value by using DataSnapshot member methods?

推荐答案

我似乎正在为具有多个子级的对象添加值侦听器.我猜这是一个 limitToLast(1)查询,但是如果您在问题中包含代码,那将非常有帮助.但是,在您添加它之前,这是我对您的代码外观的猜测:

I looks like you're adding a value listener to something that has multiple children. I'm guessing it's a limitToLast(1) query, but it would be really helpful if you include the code in your question. Until you do add it however, this is my guess of what your code looks like:

Firebase ref = new Firebase("https://yours.firebaseio.com");
Query messages = ref.orderByKey().limitToLast(1);
messages.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i("Messages", dataSnapshot.toString());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
})

虽然将过滤器限制为单个邮件,但您仍然是邮件的列表.这只是一条消息的列表.

While you are limiting the filter to single message, you are still a list of messages. It's just a list of only 1 message.

这意味着在 onDataChange 上,您仍然需要向下钻取到子级.由于您无法按索引执行此操作,因此需要使用 for 循环:

This means that on onDataChange you will still need to drill down into the child. Since you cannot do that by index, you will need to use a for loop:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
        String message = (String) chatSnapshot.child("message").getValue();
        String sender = (String) chatSnapshot.child("sender").getValue();
    }
}

请注意,如果您花一点时间创建一个代表消息的Java类,这将使它更具可读性:

Note that this gets a lot more readable if you take a moment to create a Java class that represents the message:

public class Chat {
    String message;
    String sender;
    public String getMessage() { return message; }
    public String getSender() { return sender; }
}

然后您可以通过以下方式获取消息:

Then you can get the message with:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot chatSnapshot: dataSnapshot.getChildren()) {
        Chat chat = chatSnapshot.getValue(Chat.class);
        String message = chat.getMessage();
        String sender = chat.getSender();
    }
}

上面的代码段仍然需要 for 循环,因为您仍然会收到1条消息的列表.如果要摆脱该循环,可以注册一个子事件侦听器:

The above snippet still needs the for loop, since you're still getting a list of 1 messages. If you want to get rid of that loop, you can register a child event listener:

query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Chat chat = dataSnapshot.getValue(Chat.class);
        String message = chat.getMessage();
        String sender = chat.getSender();
    }

    // other overridden messages have been removed for brevity

})

Firebase将为每个孩子调用 onChildAdded()方法,因此您不必亲自检查它们.

Firebase will call the onChildAdded() method for each child, so you don't have to look over them yourself.

这篇关于FireBase查询.数据快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆