从Android中的数组中的Firebase获取数据 [英] Getting data from firebase in array in Android

查看:46
本文介绍了从Android中的数组中的Firebase获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的新手.我在firebase中有以下数据:

I am new to Firebase. I have following data in firebase:

在EventPlayer内部,我有一组用于关键eventID的数据.我想获取一个数组中的eventID的所有对象.

Inside EventPlayer I have array of data for key eventID. I want to get all objects for a eventID in an Array.

我的代码:

final String eventId=intent.getStringExtra("EventID");
    mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
    // Attach a listener to read the data at our posts reference
    mref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                EventRequest post = child.getValue(EventRequest.class);
                if(post != null){
                    System.out.println(post);
                    arrPlayers.add(post);
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

该帖子没有任何价值,它是一个空类.

The post has no value and it is an empty class.

创建差旅清单的代码:

arrPlayers = new ArrayList<EventRequest>();

推荐答案

尝试此代码.唯一的变化是前几行带有postQuery变量.

Try this code. The only change is in the first few lines, with the postQuery variable.

final String eventId=intent.getStringExtra("EventID");
Query postQuery = mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
    // Attach a listener to read the data at our posts reference
    postQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                EventRequest post = child.getValue(EventRequest.class);
                if(post != null){
                    System.out.println(post);
                    arrPlayers.add(post);
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

您的第二行代码会创建一个 Query 对象,但是您绝不会将其分配给任何对象.将其分配给新变量,然后将 ValueEventListener 添加到该新变量中,应该可以使您了解数据.

Your second line of code creates a Query object, but you never assign it to anything. Assigning it to a new variable and then adding the ValueEventListener to that new variable should get you to your data.

这篇关于从Android中的数组中的Firebase获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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