Kareem Elsayed Kareem Elsayed - 11 months ago 144
Android Question

how to get data from firebase database

i am new to firebase..

I am trying to assign more info about users register in my app and as name,password,email,image are not enough for each user. so i decided to use the UID as a key to each user and store that in database then i can grab later.
so if the user login any other time i want retrieve this data these data wont be changed again so setvalue() and add listener to change wont work with me so in order to do so i coded

Query q = FirebaseDatabase.getInstance().getReference().child("users").equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());

what should i do next?!

the guide doesnt say any info about equalto(),how to use query or how to retrieve data without listeners



Firebase retrieval queries are asynchronous, there is no "official" way of doing it synchronous (without callbacks/listeners).

Why is it Asynchronous?

Because Firebase is cloud-based, it needs to do network calls to do any CRUD operation.

Now that you have your Query object, you can simply attach a ValueEventListener and receive your data in a DataSnapShot.

Note: addListenerForSingleValueEvent(listener) only queries once, in oppose to addValueEventListener(listener).

it would look like this :

        new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                // for example: if you're expecting your user's data as an object of the "User" class.
                User user = dataSnapshot.getValue(User.class);

            public void onCancelled(DatabaseError databaseError) {
                // read query is cancelled.

for more info refer to this Link

Edit: Event listeners are initially triggered once, then triggered whenever the data you're querying on changes.