如何从firebase检索特定的数据列表

时间:2022-01-11 00:06:36

I am new to firebase and nosql database

我是firebase和nosql数据库的新手

I am trying to retrieve some specific data from a Firebase. I have a node universities and that node has many unique identifier as node which has so more data into it. I want to retrieve name from each node.

我正在尝试从Firebase中检索某些特定数据。我有一个节点大学,该节点有许多唯一标识符作为节点,它有更多的数据。我想从每个节点检索名称。

Please have a look at this.

请看看这个。

如何从firebase检索特定的数据列表

What I have tried so far: I tried using addChildEventListener but this only listens first child. I've debugged it and it was only showing the values of the first child of universities node.

到目前为止我尝试过:我尝试使用addChildEventListener,但这只是监听第一个孩子。我调试了它,它只显示了大学节点的第一个孩子的值。

myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            List<University> university = new ArrayList<>();
            Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();

            Log.d("TAG",dataSnapshot.getValue().toString());
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I have also tried addValueEventListener. This listens the whole node and return whole data, but I am unable to extract "name" for it since it contains unique identifiers.

我也试过addValueEventListener。这会侦听整个节点并返回整个数据,但我无法为其提取“名称”,因为它包含唯一标识符。

Please guide me into the right directions.

请引导我进入正确的方向。

myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                University university = dataSnapshot.getValue(University.class);
                List<University> universities = (List<University>) dataSnapshot.getValue(University.class);
                *//*List<String> lst = new ArrayList<String>();

            for(DataSnapshot dsp : dataSnapshot.getChildren()){
                lst.add(dsp.getKey());
            }
            Log.d("TAG",lst.toString());*//*

            Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();


            *//*List<Object> values = new ArrayList<Object>(td.values());
            List<String> list=new ArrayList<String>();
            for(int i=0; i<values.size(); i++){
                list.add((String) values.get(i));
            }*//*
            Log.d("TAG", university.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2 个解决方案

#1


14  

If you using addValueEventListener for retrieve all university from Firebase

如果您使用addValueEventListener从Firebase检索所有大学

List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        universityList.clear();
        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            University university = postSnapshot.getValue(University.class);
            universityList.add(university);

            // here you can access to name property like university.name

        }
    }

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

Or if you using addChildEventListener

或者如果您使用addChildEventListener

List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
         University university = dataSnapshot.getValue(University.class);
         universityList.add(university);
         Log.i(TAG,"add university name = " + university.name);
         ...
    }
    ...
}

#2


1  

You can use equalTo() of Query in which you can pass your custom string to match with firebase database.

您可以使用Query的equalTo(),您可以在其中传递自定义字符串以与firebase数据库匹配。

like this way you can create your Query and set listener with that

像这样你可以用它创建你的Query并设置监听器

Query queryRef = myFirebaseRef.orderByChild("name").equalTo("some name");

refer this thread for more.

请参阅此主题了解更多信息

Here is nice example from firebase officials, refer that and you will get clear idea

这是来自firebase官员的很好的例子,请参考,你会得到清晰的想法

#1


14  

If you using addValueEventListener for retrieve all university from Firebase

如果您使用addValueEventListener从Firebase检索所有大学

List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        universityList.clear();
        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            University university = postSnapshot.getValue(University.class);
            universityList.add(university);

            // here you can access to name property like university.name

        }
    }

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

Or if you using addChildEventListener

或者如果您使用addChildEventListener

List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
         University university = dataSnapshot.getValue(University.class);
         universityList.add(university);
         Log.i(TAG,"add university name = " + university.name);
         ...
    }
    ...
}

#2


1  

You can use equalTo() of Query in which you can pass your custom string to match with firebase database.

您可以使用Query的equalTo(),您可以在其中传递自定义字符串以与firebase数据库匹配。

like this way you can create your Query and set listener with that

像这样你可以用它创建你的Query并设置监听器

Query queryRef = myFirebaseRef.orderByChild("name").equalTo("some name");

refer this thread for more.

请参阅此主题了解更多信息

Here is nice example from firebase officials, refer that and you will get clear idea

这是来自firebase官员的很好的例子,请参考,你会得到清晰的想法