0

I'am trying to store the query result in a String variable, but the result I'm getting is the string conversion of the query object.

tableDatabaseReference = FirebaseDatabase.getInstance().getReference("tables");
tableNameQuery = tableDatabaseReference.child("tableName").orderByChild(tableID).equalTo("123");
String tableName = tableNameQuery.toString();

Any suggestions?

EDIT: Database structure:

Tables: -------TableID: --------------TableName: --------------TableID:

  • 1
    Above code your trying just changing the query to String. It data type conversion. Your not receiving any data so i suggest please post the database structure so we can provide answer to your question. – Ashish Jan 14 '20 at 06:03
  • `I'am trying to store the query result in a String variable` for this you need to add query result listener on Query – ρяσѕρєя K Jan 14 '20 at 06:08
  • I added my database structure – Mayur Sadhu Jan 14 '20 at 06:08
  • I did try with addValueEventListener(), but once I store the result in a variable, I can't access it from outside the onDataChange method. – Mayur Sadhu Jan 14 '20 at 06:11
  • @MayurSadhu Please check my answer below to get data from firebase. To access data outside onDataChange you have to either store data in shared pref or sqlite database once using singleValueEventListener or valueEventListener – xaif Jan 14 '20 at 06:17
  • @MayurSadhu That is expected behavior. Data is loaded from Firebase asynchronously, since it may take some time. Once the data is loaded, your `onDataChange` method is called. Any code that needs the data from the database must be inside the `onDataChange` method, or be called from there. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Jan 14 '20 at 14:40

2 Answers2

1

To retrieve data from Firebase Realtime database , you have to add a valueEventListener to your database reference variable like this

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dataRef = database.getReference("YOUR_DATABASE_REF");

dataRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
  // here you can get your data from this snapshot object
     String data = dataSnapshot.getValue("CHILD_NAME_HERE").toString();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

To get data once you can use singleValueEventListener

dataRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
 // here you can get your data from this snapshot object
    String data = dataSnapshot.getValue("CHILD_NAME_HERE").toString();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
xaif
  • 553
  • 6
  • 27
0

Firebase operation is asynchronous, so you have to wait to complete the operation to get result. To access data outside of onDataChange you can use LiveData and observe it outside. When you get data inside onDataChange then notify LiveData by posting value. Check below:

private MutableLiveData<String> tableName = new MutableLiveData<>();

DatabaseReference tableDatabaseReference = FirebaseDatabase.getInstance().getReference("tables");
tableDatabaseReference
        .child(tableID)
        .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String data = dataSnapshot.child("tableName").getValue(String.class);

        //Now post that value to LiveData

        tableName.postValue(data);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Observe data here

tableName.observe(this, name -> {
    // use name here
});
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46