gpt4 book ai didi

java - Firebase 全局保存 query.addValueEventListener 数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:57 25 4
gpt4 key购买 nike

我想将从查询中检索到的数据保存到全局类变量,例如 .RoomType,但是当它存在于 query.addValueEventListener 的范围时,数据永远不会保存。当我在其范围内执行 Log.w("Value", "value is "+ RoomType ); 时,它会输出检索到的数据。我如何在全局范围内保存这些数据?

这是我的代码:

public class DatabaseManager {


public static String RoomName, RoomType, BuildingName;
static int RoomLevel;
public Double RoomLat, RoomLang ;




//connecting to Firebase database by creating database reference
private static FirebaseDatabase mDatabase;

public static FirebaseDatabase getDatabase() {
if (mDatabase == null) {
//Retrieving an instance of the database
mDatabase = FirebaseDatabase.getInstance();

//Enabling database for offline use for events where there is no internet connection
mDatabase.setPersistenceEnabled(true);

}
return mDatabase;
}




public void getresults(DatabaseReference myref, String data){
Query query= myref.child("rooms").orderByChild("RoomName").equalTo(data);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {




for (DataSnapshot Node : dataSnapshot.getChildren()) {



RoomType = Node.child("RoomType").getValue().toString();
BuildingName = Node.child("BuildingName").getValue().toString();
RoomLevel = Node.child("RoomLevel").getValue(Integer.class);
RoomLat = Node.child("RoomLat").getValue(Double.class);
RoomLang = Node.child("RoomLang").getValue(Double.class);


}

}

Log.w("Value", " value is "+ RoomType );
}

@Override
public void onCancelled(DatabaseError databaseError) {

}


});

Log.w("Value at end ", " value is "+ RoomType );


}


}

最佳答案

尝试像这样初始化 RoomType 变量 RoomType = "";

关于java - Firebase 全局保存 query.addValueEventListener 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047170/

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