I want to get my wgID from my Firebase RealTime Database, it is located as a value of /WG/FlatNumber/ Now here is my Code:
MainActivity:
System.out.println("1");
dbContact.schreibeFlatObjekt();
System.out.println("7");
schreibeFlatObjekt:
//wgID is global
private String wgID;
public schreibeFlatObjekt(){
//CountDownLatch to wait for the Listener
final CountDownLatch cdl = new CountDownLatch(1);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("3");
//Getting the Data from the snapshot
wgID = (String) dataSnapshot.getValue();
System.out.println("4 wgID: " + wgID);
//counting down the cdl so the cdl.await() can go on
cdl.countDown();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
System.out.println("2");
fref.child("WG").child("FlatNumber").addListenerForSingleValueEvent(listener);
System.out.println("5");
try{
cdl.await();
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("6 wgID: " +wgID);
return 0;
So when I don't use the CountdownLatch, the System.out.println
give me this:
I/System.out: 1
I/System.out: 2
I/System.out: 5
I/System.out: 6 wgID: null
I/System.out: 7
I/System.out: 3
I/System.out: 4 wgID: 1
So as I see it, the Listener is executed, after the method is already ready.
If I use the CountDownLatch, it only runs until it comes to "cdl.await()" and then just stops, the Listener is never executed and the app stops.
So how do I get my ID, I mean I just want one Value from my Database, you can set it with "setValue()" but you cannot read it with "getValue" or something, it has to be done with listeners?