gpt4 book ai didi

java - 等到 firebase 查询中的 onChildAdded 完成,Android

转载 作者:搜寻专家 更新时间:2023-11-01 08:37:25 24 4
gpt4 key购买 nike

我是 Firebase 的新手,正在做一个简单的项目。我有一种方法可以从 Firebase 获取教师列表并将他们的电子邮件添加到 ArrayList 并返回。

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 变量。

例如我的 Android Monitor 就是这样的:

D/RETURN: []

D/TEACHER: {number=1, name=Teacher, email=teacher@new.lk, subject=Physics}

D/ENTRY: : DATA teacher@new.lk

有什么方法可以等到 onChildAdded 方法完成......?

============================================= ==========================

我也试过这个方法。但随后它在内部方法处停止了。

即使它没有转到 Log.d("TEACHER", ""+ value.toString()); 标签这里。

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;
}

最佳答案

public class MainActivity extends AppCompatActivity{
public static Bus bus;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bus = new Bus(ThreadEnforcer.MAIN);
bus.register(this);

//CALL TO LOAD DATA FROM FIREBASE
loadData();
}

private void loadData(){
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("my-data").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//CREATE YOUR OWN OBJECT USING RECEIVED DATA
MyObject o = new MyObject();
//CALL FOR METHOD nextMethod()
bus.post(o);
}

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

@Subscribe
private void nextMethod(MyObject o){

}
}

这是使用 otto - EventBus 的一个非常基本的示例。从 bus.post(o),您可以调用相关的订阅方法。

关于java - 等到 firebase 查询中的 onChildAdded 完成,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35644588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com