简体   繁体   English

如何在Firebase数据库中从子级检索特定数据

[英]How to retrieve particular data from the child in Firebase databse

I am new to Firebase database and my data structure in Firebase is like this: 我是Firebase数据库的新手,我在Firebase中的数据结构如下:

Root- users-sec_a, sec_b, sec_c

In each section(sec_a, sec_b, sec_c) there will be uids of users. 在每个部分(sec_a,sec_b,sec_c)中,都会有用户的uid。 in each section there is min of 40 uids. 在每个部分中,至少有40个uid。 And in all uids there is a common data child's (name , rollno , attendance,) Now my question was I want to display only names and rollno of all uids present in one of sections(section to be displayed was inputted by user). 并且在所有uid中都有一个公共数据子对象(名称,rollno,出勤率),现在我的问题是我只想显示一个部分中存在的所有uid的名称和rollno(要显示的部分由用户输入)。

How can I get that data? 我如何获得这些数据?

it's fairly easy. 这很容易。

String sectionSelectedByUser = "sec_a"; // For example

Now getting refrence of the firebase database 现在得到Firebase数据库的认可

if (FirebaseAuth.getInstance().getCurrentUser() != null){ // if you need user to be signed in.. 
            FirebaseDatabase.getInstance().getReference().child("users").child(sectionSelectedByUser).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { // iterates through all your UID in this section
                        if (childSnapshot.hasChild("name")){ // if current uid has name then fetch it
                            String name = childSnapshot.child("name").getValue().toString();
                        }

                        if (childSnapshot.hasChild("rollno")){ // if current UID has rollno then fetch it
                            String rollno =  childSnapshot.child("rollno").getValue().toString();
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }

You can retrieve nested data with child method of Fire base database. 您可以使用Fire base数据库的子方法检索嵌套数据。

For you problem you can use this code with little modification as per requirements. 对于您的问题,您可以按要求使用此代码,而无需进行任何修改。

ValueEventListener singleEventListener = new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot != null) {
            Student student = dataSnapshot.getValue(Student.class);

            Log.d("TAG","name: "+student.getName());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d("onCancelled - ", databaseError.toException());
    }
};

if (mFirebaseDatabaseReference == null) {
    mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    //mFirebaseDatabaseReference.keepSynced(true);
}
mFirebaseDatabaseReference = mFirebaseDatabaseReference.child(Constant_String.Table_User);
mFirebaseDatabaseReference.addListenerForSingleValueEvent(singleEventListener);

Hope this will help... 希望这会有所帮助...

Assuming that your database schema looks like this: 假设您的数据库架构如下所示:

Firebase-root
   |
   --- users
         |
         --- sec_a
         |    |
         |    --- uid
         |    |    |
         |    |    --- rollno: "Roll Number"
         |    |    |
         |    |    --- name: "User Name"
         |    |    |
         |    |    --- attendance: true
         |    |
         |    --- //other 40 users
         |
         --- sec_b
         |
         --- sec_c

And if you want for example to display only names and rollno of all uids present in one of sections (eg sec_a ), please use the following code: 并且,例如,如果您只想显示部分之一(例如sec_a )中存在的所有uid的名称和rollno,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference sectionRef = rootRef.child("users").child("sec_a");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            String rollno = ds.child("rollno").getValue(String.class);
            Log.d(TAG, name + " / " + rollno);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
sectionRef.addListenerForSingleValueEvent(valueEventListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM