Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve specific list of data from firebase

I am new to firebase and nosql database

I am trying to retrieve some specific data from a Firebase. I have a node universities and that node has many unique identifier as node which has so more data into it. I want to retrieve name from each node.

Please have a look at this.

enter image description here

What I have tried so far: I tried using addChildEventListener but this only listens first child. I've debugged it and it was only showing the values of the first child of universities node.

myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            List<University> university = new ArrayList<>();
            Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();

            Log.d("TAG",dataSnapshot.getValue().toString());
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I have also tried addValueEventListener. This listens the whole node and return whole data, but I am unable to extract "name" for it since it contains unique identifiers.

Please guide me into the right directions.

myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                University university = dataSnapshot.getValue(University.class);
                List<University> universities = (List<University>) dataSnapshot.getValue(University.class);
                *//*List<String> lst = new ArrayList<String>();

            for(DataSnapshot dsp : dataSnapshot.getChildren()){
                lst.add(dsp.getKey());
            }
            Log.d("TAG",lst.toString());*//*

            Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();


            *//*List<Object> values = new ArrayList<Object>(td.values());
            List<String> list=new ArrayList<String>();
            for(int i=0; i<values.size(); i++){
                list.add((String) values.get(i));
            }*//*
            Log.d("TAG", university.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
like image 277
Zeeshan Shabbir Avatar asked Jul 29 '16 06:07

Zeeshan Shabbir


People also ask

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do you filter data in Firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .


2 Answers

If you using addValueEventListener for retrieve all university from Firebase

List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        universityList.clear();
        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            University university = postSnapshot.getValue(University.class);
            universityList.add(university);

            // here you can access to name property like university.name

        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

Or if you using addChildEventListener

List<University> universityList = new ArrayList<>();
myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
         University university = dataSnapshot.getValue(University.class);
         universityList.add(university);
         Log.i(TAG,"add university name = " + university.name);
         ...
    }
    ...
}
like image 184
Linh Avatar answered Oct 14 '22 12:10

Linh


You can use equalTo() of Query in which you can pass your custom string to match with firebase database.

like this way you can create your Query and set listener with that

Query queryRef = myFirebaseRef.orderByChild("name").equalTo("some name");

refer this thread for more.

Here is nice example from firebase officials, refer that and you will get clear idea

like image 25
Ravi Avatar answered Oct 14 '22 13:10

Ravi