Firebase获取所需数据

时间:2018-12-09 06:18:35

标签: database firebase

我想检索所有子节点的电子邮件地址,但是当我执行以下命令时,它仅显示最后一个子节点的电子邮件地址。 这是Firebase的数据:

data

这是我用来检索数据的代码:

#include <algorithm>
#include <ctime>
#include <iostream>

int main() {
    int data[32768]; const int l = sizeof data / sizeof data[0];

    for (unsigned c = 0; c < l; ++c)
        data[c] = std::rand() % 256;

    // sort 200-element segments, not the whole array
    for (unsigned c = 0; c + 200 <= l; c += 200)
        std::sort(&data[c], &data[c + 200]);

    clock_t start = clock();
    long long sum = 0;

    for (unsigned i = 0; i < 100000; ++i) {
        for (unsigned c = 0; c < sizeof data / sizeof(int); ++c) {
            if (data[c] >= 128)
                sum += data[c];
        }
    }

    std::cout << static_cast<double>(clock() - start) / CLOCKS_PER_SEC << std::endl;
    std::cout << "sum = " << sum << std::endl;
}

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

FirebaseDatabase.getInstance().getReference().child("user").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for (DataSnapshot child : children) {
                User value = child.getValue(User.class);

                if (value != null) {
                    Toast.makeText(MainActivity.this, "List of emails: " + value.getEmail(), Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

User类是基本的POJO类:

public class User {
public String name,email,password,userType;
public int lattitude,longitude;

public String getEmail() {
    return name;
}

public void setEmail(String name) {
    this.name = name;
}