Skip to content Skip to sidebar Skip to footer

W/classmapper: No Setter/field For Class

I'm simply just trying to populate data from Firebase Database into my recyclerview. My recyclerview shows perfectly, but the adapter won't set the values to the text in recyclervi

Solution 1:

Even if I don't see your database structure, I can say from your code that your asking for everything under Street Problems, which includes all of the objects you pushed previously. I can see from the error message that it's trying to find a setter or field for the push ID -L57t4-97c3dLZjA_yXC that it found just under Street Problems node. If you want to get a single object, you're going to have to dig into the objects in the push IDs under Street Problems. Your code should look something like this:

dataSnapshot.child("Street Problems/-L57t4-97c3dLZjA_yXC").getValue(StreetClass.class);

If you want all StreetClass objects, then simply loop through its childrens like this:

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceyourRef= rootRef.child("Street Problems");
ValueEventListenereventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                StreetClassstreetClass= dSnapshot.getValue(StreetClass.class);
                Log.d("TAG", streetClass.getName());
            }
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

This is an example on how to display the names using getName() method.

Edit:

As i see on your updatedquestion, the warning is because the casing mismatches between your field and you setter.

You have a field named and the corresponding setters and getters which are not correct!

privateString sname;

publicStringgetName() {
    return sname;
}

publicvoidsetName(String name) {
    this.sname = name;
}

The correct setters and getters should be:

publicStringgetSname() {
    return sname;
}

publicvoidsetSname(String name) {
    this.sname = name;
}

The same problems is also with the other fields.

This is the correct way of structuring your model class:

publicclassStreetClass {
    privateString id;
    privateString semail;
    privateString sname;
    privateString stype;
    privateString sdetail;
    privateString slocation;
    privateString sdate;
    privateString imgurl;

    publicStreetClass(){}

    publicStreetClass(String id, String semail, String sname, String stype, String sdetail, String slocation, String sdate, String imgurl) {
        this.id = id;
        this.semail = semail;
        this.sname = sname;
        this.stype = stype;
        this.sdetail = sdetail;
        this.slocation = slocation;
        this.sdate = sdate;
        this.imgurl = imgurl;
    }

    publicStringgetId() {
        return id;
    }

    publicvoidsetId(String id) {
        this.id = id;
    }

    publicStringgetSemail() {
        return semail;
    }

    publicvoidsetSemail(String semail) {
        this.semail = semail;
    }

    publicStringgetSname() {
        return sname;
    }

    publicvoidsetSname(String sname) {
        this.sname = sname;
    }

    publicStringgetStype() {
        return stype;
    }

    publicvoidsetStype(String stype) {
        this.stype = stype;
    }

    publicStringgetSdetail() {
        return sdetail;
    }

    publicvoidsetSdetail(String sdetail) {
        this.sdetail = sdetail;
    }

    publicStringgetSlocation() {
        return slocation;
    }

    publicvoidsetSlocation(String slocation) {
        this.slocation = slocation;
    }

    publicStringgetSdate() {
        return sdate;
    }

    publicvoidsetSdate(String sdate) {
        this.sdate = sdate;
    }

    publicStringgetImgurl() {
        return imgurl;
    }

    publicvoidsetImgurl(String imgurl) {
        this.imgurl = imgurl;
    }
}

You can also use the String class as below:

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceyourRef= rootRef.child("Street Problems");
ValueEventListenereventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                Stringsname= dSnapshot.child("sname").getValue(String.class);
                Log.d("TAG", sname);
            }
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

Post a Comment for "W/classmapper: No Setter/field For Class"