W/ClassMapper: No setter/field for class

后端 未结 1 514
耶瑟儿~
耶瑟儿~ 2021-02-06 17:31

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 recyc

相关标签:
1条回答
  • 2021-02-06 18:01

    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:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference yourRef = rootRef.child("Street Problems");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                for(DataSnapshot dSnapshot : ds.getChildren()) {
                    StreetClass streetClass = dSnapshot.getValue(StreetClass.class);
                    Log.d("TAG", streetClass.getName());
                }
            }
        }
    
        @Override
        public void onCancelled(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!

    private String sname;
    
    public String getName() {
        return sname;
    }
    
    public void setName(String name) {
        this.sname = name;
    }
    

    The correct setters and getters should be:

    public String getSname() {
        return sname;
    }
    
    public void setSname(String name) {
        this.sname = name;
    }
    

    The same problems is also with the other fields.

    This is the correct way of structuring your model class:

    public class StreetClass {
        private String id;
        private String semail;
        private String sname;
        private String stype;
        private String sdetail;
        private String slocation;
        private String sdate;
        private String imgurl;
    
        public StreetClass(){}
    
        public StreetClass(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;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getSemail() {
            return semail;
        }
    
        public void setSemail(String semail) {
            this.semail = semail;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public String getStype() {
            return stype;
        }
    
        public void setStype(String stype) {
            this.stype = stype;
        }
    
        public String getSdetail() {
            return sdetail;
        }
    
        public void setSdetail(String sdetail) {
            this.sdetail = sdetail;
        }
    
        public String getSlocation() {
            return slocation;
        }
    
        public void setSlocation(String slocation) {
            this.slocation = slocation;
        }
    
        public String getSdate() {
            return sdate;
        }
    
        public void setSdate(String sdate) {
            this.sdate = sdate;
        }
    
        public String getImgurl() {
            return imgurl;
        }
    
        public void setImgurl(String imgurl) {
            this.imgurl = imgurl;
        }
    }
    

    You can also use the String class as below:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference yourRef = rootRef.child("Street Problems");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                for(DataSnapshot dSnapshot : ds.getChildren()) {
                    String sname = dSnapshot.child("sname").getValue(String.class);
                    Log.d("TAG", sname);
                }
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    yourRef.addListenerForSingleValueEvent(eventListener);
    
    0 讨论(0)
提交回复
热议问题