0

I'm trying to retrieve data in realtime from my firebase to my app. So, every time I try to change data(like name, ratio, and total_slot) in firebase, it should show it on my app as well in real time. However, it's a bit difficult to get the keys in my firebase structure. If anyone can help though. It would mean so much.

Attached here is my Firebase data structure Firebase Database Structure

My Model class

public class LocationModel {
    Long lat,lng,ratio,total_slot;
    String name;
    String key;

    public LocationModel(){
    }

    public Long getLat(){return lat;}
    public void setLat(Long lat){this.lat=lat;}

    public Long getLng(){return lng;}
    public void setLng(Long lng){this.lng=lng;}

    public Long getRatio(){ return ratio;}
    public void setRatio(Long ratio){this.ratio = ratio;}

    public Long getTotal_slot(){ return total_slot;}
    public void setTotal_slot(Long total_slot){this.total_slot = total_slot;}

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;
    }

    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
}

How I try to retrieve the data I changed in the firebase

//RETRIEVE
public ArrayList<LocationModel> retrieve()
{
    db.child("locs").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
            LocationModel locationModel = dataSnapshot.getValue(LocationModel.class);

            locationModels.add(locationModel);

            String key = dataSnapshot.getKey();
            mKeys.add(key);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            for(DataSnapshot ds: dataSnapshot.getChildren()){
                LocationModel locationModel = dataSnapshot.getValue(LocationModel.class);
                String key = ds.getKey();
                int index=mKeys.indexOf(key);
                locationModels.set(index, locationModel);
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

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

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return locationModels;
}

However, everytime I try to change data on my firebase, it gives me an error Maybe its because of the key. If anyone can help please

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
mppaler
  • 1
  • 1
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Vishal Yadav Mar 13 '18 at 07:24
  • 2
    Why did you remove the error from the question? This can be very helpfull for helping you resolve this issue. – André Kool Mar 13 '18 at 09:15
  • Possible duplicate of [How to return dataSnapshot value as a result of a method?](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method) – Alex Mamo Mar 13 '18 at 13:11
  • Get rid of the children loop on 'onChildChanged' ; the Location object is the child so use fetchData; also the key will now be the same as when you added. Also , if you implement 'equals' on the Location class to compare "keys" then you dont' need a separate 'mKeys' and you can instead use 'indexOf()' on the 'set' command. Should still check for -1 just in case. Btw, the stack trace (found in the edit history) is super useful. – Andy Mar 13 '18 at 14:27
  • Also your latitude and longitude values cannot be treated as 'Long's. – Andy Mar 13 '18 at 14:37
  • @Andy I did everything that you said and it worked like a charm! Thank you so much for your help! I still used the mKeys though, it doing what its supposed to do. Again, thank you! P.S I'm only getting the name, ratio, and total_slot for my list view. But, lat and lng should be?? Double? – mppaler Mar 13 '18 at 15:00
  • "But, lat and lng should be?? Double? " .... yes. – Andy Mar 13 '18 at 15:09

0 Answers0