skip to Main Content

I try to save of my SQLite database some records from the Firebase database.

My code to access the firebase and to store the information’s in an ArrayList is:

private List<PoIs> LocoList;


LocoList = new ArrayList<>();

FirebaseApp.initializeApp(this);
FirebaseAuth mAuth = FirebaseAuth.getInstance();
ref = FirebaseDatabase.getInstance().getReference("DATABASE");
ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            LocoList.clear();
            if (dataSnapshot.exists()) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    PoIs loco = dataSnapshot.getValue(PoIs.class);
                    LocoList.add(loco);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);

The PoIs class looks like this:

public class PoIs {
private Integer _location_id;
private String _location_name;
private String _location_address;
private String _location_description;
private String _location_county;

public PoIs(){}

public PoIs(Integer _location_id, String _location_name, String _location_address,String _location_description, String _location_county) {
    this._location_id = _location_id;
    this._location_name = _location_name;
    this._location_address = _location_address;
    this._location_description = _location_description;
    this._location_county = _location_county;
}


@PropertyName("_location_description")
public String get_location_description() {
    return _location_description;
}

@PropertyName("_location_description")
public void set_location_description(String _location_description) {
    this._location_description = _location_description;
}

My firebase structure look like this:

{
"DATABASE": {


"PoI_100": {
"_location_address": "Comuna Sâmbăta ",
"_location_county": "BRASOV",
"_location_description": "N/A",
"_location_id": 100,
"_location_name": "Mănăstirea Sâmbăta (Brâncoveanu)",
},
"PoI_1000": {
"_location_address": "Costinești ",
"_location_county": "CONSTANTA",
"_location_description": "N/A",
"_location_id": 1000,
"_location_name": "Teatrul de Vară",
},
"PoI_1001": {
"_location_address": "Olimp",
"_location_county": "CONSTANTA",
"_location_description": "N/A",
"_location_id": 1001,
"_location_image": "-",
"_location_name": "Plaja Olimp",
},

...

All are in a child node named "database".

Unfortunately, I get a warning like no setter/field exists for my class. Any idea?

2

Answers


  1. Chosen as BEST ANSWER

    Problem was here:

    if (dataSnapshot.exists()) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
             ///// HERE: I used dataSnapshot instead ds
             PoIs loco = dataSnapshot.getValue(PoIs.class);
             /////
             LocoList.add(loco);
        }
    }
    

    Was enough to replace with

    PoIs loco = ds.getValue(PoIs.class);
    

  2. You’re getting the following warning:

    No setter/field warning

    Because the names of the fields in your class are different than the ones in the database. For example, in your class, you have a field called location_address while in the database is called _location_address. See the _ (underscore) with which the field starts? That’s the reason why you get that warning. To solve this, you either modify the names of the fields in the class to match the ones in the database, or you use an annotation in front of the public getter slike this:

    @PropertyName("_location_address") //👈
    public String getLocation_address() {
        return location_address;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search