我正在尝试获取类似SQL left join子句的数据。 这是我的节点样本:
Firebase数据库
{
"teachers": {
"nodeKey01": {
"name":"captain hook",
"branch":"Math",
},
"nodeKey02": {
"name":"peter pan",
"branch":"Science",
}
},
"students": {
"nodeKey100": {
"name" : "davy jones",
"teacherKey": "nodeKey01"
},
"nodeKey101": {
"name" : "jack sparrow",
"teacherKey": "nodeKey02"
},
"nodeKey102": {
"name" : "salazar",
"teacherKey": "nodeKey01"
}
}
}
我想查询“学生”的依据并获得“教师”名称和分支的数据。我引用了文档,很多例子都没弄清楚。 Firebase不包括SQL数据库语言,但必须有一种方法/方法来加入节点。
型号:
public class Student {
private String name;
private String teacherKey;
private String key;
private Teacher teacher;
public Student() {}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put(name, name);
result.put(teacherKey, teacherKey);
return result;
}
public String getKey() { return key;}
public void setKey(String objectKey) { this.key = key; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getTeacherKey() { return teacherKey; }
public void setTeacherKey(String teacherKey) { this.teacherKey = teacherKey; }
public Teacher getTeacher() { return teacher; }
public void setTeacher(Teacher teacher) { this.teacher = teacher; }
}
public class Teacher {
private String name;
private String branch;
private String key;
public Teacher() {}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put(name, name);
result.put(branch, branch);
return result;
}
public String getKey() { return key;}
public void setKey(String objectKey) { this.key = key; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getBranch() { return branch; }
public void setBranch(String branch) { this.branch = branch; }
}
Firebase查询代码:
public class StudentsFragment extends Fragment {
List<Students> listData = new ArrayList<>();
StudentAdapter adapter;
@BindView(R.id.list) ListView _list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_students, container, false);
ButterKnife.bind(this, view);
mDatabase = FirebaseDatabase.getInstance().getReference();
adapter = new StudentAdapter(getActivity(), listData);
_list.setAdapter(adapter);
initDatabase();
return view;
}
private void initDatabase() {
final List<Teacher> teachers = new ArrayList<>();
Query query1 = mDatabase.child("teachers");
query1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
teachers.clear();
for(DataSnapshot data : dataSnapshot.getChildren()){
Teacher item = data.getValue(Teacher.class);
item.setKey(data.getKey());
teachers.add(item);
}
Query query2 = mDatabase.child("students");
query2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listData.clear();
for (DataSnapshot data: dataSnapshot.getChildren()) {
AbsentTeacher item = data.getValue(Student.class);
item.setKey(data.getKey());
item.setTeacher(getTeacherByKey(teachers, item.getTeacherKey()));
listData.add(item);
}
adapter.notifyDataSetChanged();
}
private Teacher getTeacherByKey(List<Teacher> list, String key) {
int index = 0;
for (int i=0;i<list.size();i++){
if (list.get(i).getObjectKey().equals(key)) {
index = i;
break;
}
}
return list.get(index);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
}
它正在运行,但我正在加入带有两个firebase查询和“for循环”的节点......这似乎是一个性能问题。