我想将从查询中检索到的数据保存到全局类变量,例如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 );
}
}
答案 0 :(得分:0)
尝试按照 RoomType = "";