简体   繁体   English

如何从Firebase中检索包含另一个字符串列表的完整对象列表?

[英]How to retrieve full List Of Objects from Firebase that contains another List of String in it?

I have stored some List of person objects on Firebase .I want to retrieve this full List into newList of person from database.The class perosn contains another List batch My class person look like: 我已经在Firebase上存储了一些人员对象列表 。我想从数据库中将这个完整的列表检索到人员的新列表中 .perosn类包含另一个列表批次我的班级人员看起来像:

public class person {
public String name,sid;
public HashMap<String,String> batch;
public int cls;
}

The code I have used is: 我使用的代码是:

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference fDatabaseRoot = database.getReference("studentsList");
    fDatabaseRoot.orderByChild("name").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
                Map<String,person> td = 
                        (HashMap<String,person>)areaSnapshot.getValue();


               // arrayListStudent= (List<person>) td.values();
                 // Log.d("aaa1",arrayListStudent.toString());
            }
            //Log.d("aaa",arrayListStudent.toString());
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

Where am i getting wrong.Please give some hint and suggestions.... Here is the screenshot of my database structure: 我哪里出错了。请给出一些提示和建议。...这是我的数据库结构的屏幕截图:

屏幕截图

Please use this code: 请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference studentsListRef = rootRef.child("studentsList");
Query query = studentsListRef.orderByChild("name");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            String key1 = ds.child("batch").child("0").getValue(String.class);
            String key2 = ds.child("batch").child("1").getValue(String.class);
            Log.d("TAG", name + " / " + key1 + " / " + key2);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);

In this way, you'll get the value of name and of both fields under batch . 这样,您将在batch下获得name和两个字段的值。

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

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