Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

        }
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
135 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...