Firebase, get specfic chid from parent

笑着哭i 提交于 2021-02-11 13:27:00

问题


I want to match branch value in all child of student nodes with the value I already have and if matched successfully then I want to retrieve 'Student name' value in matched child see blow image


回答1:


You're looking for a query in that case.

To match the value of branch and then print the value of studentName for all matching child nodes, you'd do:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(".../students");
Query query = ref.orderByChild("branch").equalTo("Electronics Engineering");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot studentSnapshot: dataSnapshot.getChildren()) {
            Log.d(TAG, studentSnapshot.child("studentName").getValue(String.class));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}


来源:https://stackoverflow.com/questions/60405198/firebase-get-specfic-chid-from-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!