Zeeshan Shabbir Zeeshan Shabbir - 4 months ago 533
Android Question

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) {

}
});

Answer

If you using addValueEventListener for retrieve all university from Firebase

List<University> universityList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
    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

        }
    }

    public void onCancelled(FirebaseError firebaseError) {
        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) {
    @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);
         ...
    }
    ...
}