问题描述
我是Firebase的新手,正在从事简单的项目.我有一种方法可以从Firebase获取教师列表,并将他们的电子邮件添加到ArrayList并返回.
I'm new to Firebase and working on simple project. I have a method to get a list of teachers from Firebase and add their emails to an ArrayList and return.
public ArrayList<String> getTeacherList() {
temp = new ArrayList();
Firebase node = ref.child("teachers");
Query query = node.orderByChild("subject").equalTo(selected_subject);
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String,Object> value = (Map<String, Object>) dataSnapshot.getValue();
Log.d("TEACHER"," "+value.toString());
Iterator entries = value.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
String key = (String) thisEntry.getKey();
Object data = thisEntry.getValue();
if(key.equals("email")) {
temp.add(data.toString());
Log.d("ENTRY",": DATA "+data.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(FirebaseError firebaseError) {
}
});
Log.d("RETURN", temp.toString());
return temp;
}
此方法运行良好.但是问题是这会在完成添加子项之前返回 temp
变量.
This method is working fine. But the problem is this returns the temp
variable before completing adding child.
例如,我的Android Monitor就是这样:
For example my Android Monitor is like that :
D/老师:{number = 1,姓名=老师,电子邮件[email protected],主题=物理}
D/TEACHER: {number=1, name=Teacher, [email protected], subject=Physics}
D/ENTRY :: DATA老师@ new.lk
D/ENTRY: : DATA [email protected]
有什么方法可以等待,直到完成该 onChildAdded
方法.?
Is there any way to wait until complete thatonChildAdded
method..?
========================================================================
=========================================================================
我也尝试过这种方式.但是随后它停在了内部方法中.
I tried in this way also. But then it stopped at inside method.
即使没有转到 Log.d("TEACHER"," + value.toString());
标签在这里.
Even it didn't go to Log.d("TEACHER", " " + value.toString());
tag here.
public ArrayList getTeacherList() throws InterruptedException {
temp = new ArrayList();
Firebase node = ref.child("teachers");
Query query = node.orderByChild("subject").equalTo(selected_subject);
semaphore = new Semaphore(0);
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, Object> value = (Map<String, Object>) dataSnapshot.getValue();
Log.d("TEACHER", " " + value.toString());
Iterator entries = value.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
String key = (String) thisEntry.getKey();
Object data = thisEntry.getValue();
if (key.equals("email")) {
setArrayList(data.toString());
Log.d("ENTRY", ": DATA " + data.toString());
}
}
semaphore.release();
}
@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(FirebaseError firebaseError) {
}
});
semaphore.acquire();
return temp;
}
推荐答案
已解决:我从 http://square.github.io/otto/使用了Otto EventBus库,它适用于我
Solved : I used Otto EventBus Library from http://square.github.io/otto/ and it worked for me
这篇关于等到在Firebase查询中完成onChildAdded之后,Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!