Reputation: 73
I am new to using Firebase database and also slowly learning Android. Anyway I tried to store data in Firebase database and it was successful. Now I am trying to read data from Firebase database, but unfortunately I cannot do it.
I read a lot to query data from the Firebase database but still not able to get it working.
Below is the data stored in Firebase Realtime Database.
And below is my reference code which I am trying to get data.
I have connected the mobile for using adb to debug the android application.
While trying to debug, I kept breakpoint at onDataChange
but it was never hit.
I could see some information in the query value but I could not get full information of the query. The value does not contain my query result.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_request);
databaseRequests = FirebaseDatabase.getInstance().getReference("Requests");
Query query = databaseRequests.orderByChild("requestNo").equalTo(11);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Request request = dataSnapshot.getValue(Request.class);
Log.d("CREATION"," Request.no:" + request.requestNo +" Request Purpose:" + request.requestPurpose);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Let me know what is going wrong? I tried to debug a lot, but I am sure something I am missing.
Upvotes: 1
Views: 1268
Reputation: 2919
addListenerForSingleValueEvent()
executes OnDataChange
method immediately and after executing that method once it stops listening to the reference location it is attached to.
addListenerForSingleValueEvent Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.
You will need to use addValueEventListener()
, how will keep listening to query or database reference it is attached to.
addValueEventListener Add a listener for changes in the data at this location. Each time time the data changes, your listener will be called with an immutable snapshot of the data.
Link to addValueEventListener documentation
Upvotes: 2