Simranjit Singh
Simranjit Singh

Reputation: 11

How to retrieve all the users data in Firebase (android)?

Code i am using to retrieve data:

Firebase ref = new Firebase(Config.FIREBASE_URL);
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Person person = postSnapshot.getValue(Person.class);


                        //Adding it to a string
                        String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\nLatLong: "+person.getflat()+","+person.getflng()+"\nReamaining Distance: "+person.getfdist();

                        //Displaying it on textview
                        textViewPersons.setText(string);
                    }
                }

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

This is how my firebase database looks like. The problem is using the above code i'm only able to retrieve data of the second user(Simranjit Singh). I want to retrieve both the users.

6MpsYKoVMNfJMvUdE9y2oPZSnxb2addclose
   address: 
    "[email protected]"
   fdist: 
    "31.95893"
   flat: 
    "30.69074016"
   flng: 
    "76.74376851"
   name: 
    "Tintin Sharma"
fxrutNP5uLfoPGIJtTuXAYam5dP
   address: 
    "[email protected]"
   fdist: 
    "31.95893"
   flat: 
    "30.69074016"
   flng: 
    "76.74376851"
   name: 
    "Simranjit Singh"

Upvotes: 1

Views: 3292

Answers (2)

Badhrinath Canessane
Badhrinath Canessane

Reputation: 3518

You are not retrieving the data properly...you are overwriting the data in a string variable, so you will only get the second value. You need a list of users.

    private static HashMap<String, Person> personList = new HashMap<>();;
    Firebase ref = new Firebase(Config.FIREBASE_URL);
                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                            //Getting the data from snapshot
                            Person person = postSnapshot.getValue(Person.class);
    personList.put(snapshot.getKey(), person.getName());


                            //Adding it to a string
                            String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\nLatLong: "+person.getflat()+","+person.getflng()+"\nReamaining Distance: "+person.getfdist();

                            //Displaying it on textview
                            textViewPersons.setText(string);
                        }
//Return you personList here
                    }

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

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

The issue is that you are getting each user, and assigning it to the same text view, which overwrites the last person. So no matter how many you have, you will always get the last user. You should put the users into an ArrayList, or a similar data structure, and use that to populate a list view:

Firebase ref = new Firebase(Config.FIREBASE_URL);
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    ArrayList<Person> peopleList = new ArrayList<Person>():
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Person person = postSnapshot.getValue(Person.class);

                        //add person to your list
                        peopleList.add(person);
                        //create a list view, and add the apapter, passing in your list

                    }
                   yourListView.setAdapter(personList);
                }

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

Upvotes: 1

Related Questions