1

I want to save the data I retrieved from the query to global class variables e.g .RoomType but when it exists query.addValueEventListener's scope the data is never saved. When I perform Log.w("Value", " value is "+ RoomType ); within its scope it outputs the data retrieved. How would I save those data globally?

Here is my code:

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


}


}
nick122122
  • 11
  • 3
  • 1
    https://stackoverflow.com/questions/4572338/extending-application-to-share-variables-globally extend application class maybe you can find a better example – Pomagranite Jan 01 '18 at 04:54

1 Answers1

0

Try initializing the RoomType variable like this RoomType = "";