Meher Meher - 2 months ago 34
Android Question

Android Firebase query data

Im trying to query data from firebase database.Below images show data structure. enter image description here.Im trying to query messages by data(long value is the time in milli seconds).Below is the code :

database.child("Market").child("data").child("messages").orderByKey().startAt("1473777076600").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

MessageFirebaseHelper data = dataSnapshot.getValue(MessageFirebaseHelper.class);

Object[] messageKeys = data.getMessages().keySet().toArray();

for(int i=0;i<messageKeys.length;i++){
Logger.i(TAG, " messages keys : " + messageKeys[i]);
}

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


Below is the error:

W/ClassMapper(8838): No setter/field for 1473777892283 found on class com.app.MessageFirebaseHelper


As the keys change dynamically, creating getter/setter is not correct.
How do i get the data and save it ?

TRY 2:

I tried in different way, Query :

database.child("Market").child("data").orderByChild("time")
.startAt("1473777076600").addListenerForSingleValueEvent(new ValueEventListener() {


This does not return any value.

Answer

Try like this,,

public void onDataChange(DataSnapshot dataSnapshot) {

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

            yourModel mModel = eventSnapshot.getValue(yourModel.class);
            Log.e("DATA" ,""+ mModel);   
     }

}