需要帮助将Firebase数据转换为Java Object以在recyclerview中使用

时间:2021-06-04 02:02:33

I have read tons of reports and questions on this subject and can't seem to figure this out. I have a Java Object Class called Yard:

我已经阅读了大量关于这个主题的报告和问题,似乎无法弄清楚这一点。我有一个名为Yard的Java Object Class:

public class Yard {
    private String id;
    private String location;
    private long hives;
    private String last_visit;
    private String photo_url;
    private String yard_type;

    public Yard(String id, String location, long hives, String last_visit, String photo_url, String yard_type) {
        this.id = id;
        this.location = location;
        this.hives = hives;
        this.last_visit = last_visit;
        this.photo_url = photo_url;
        this.yard_type = yard_type;
    }

    public Yard() {

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public long getHives() {
        return hives;
    }

    public void setHives(long hives) {
        this.hives = hives;
    }

    public String getLast_visit() {
        return last_visit;
    }

    public void setLast_visit(String last_visit) {
        this.last_visit = last_visit;
    }

    public String getPhoto_url() {
        return photo_url;
    }

    public void setPhoto_url(String photo_url) {
        this.photo_url = photo_url;
    }

    public String getYard_type() {
        return yard_type;
    }

    public void setYard_type(String yard_type) {
        this.yard_type = yard_type;
    }

    @Override
    public String toString() {
        return "Yard{" +
                "id='" + id + '\'' +
                ", location='" + location + '\'' +
                ", hives=" + hives +
                ", last_visit='" + last_visit + '\'' +
                ", photo_url='" + photo_url + '\'' +
                ", yard_type='" + yard_type + '\'' +
                '}';
    }
}

And I am trying to retrieve the user's yards to display them in a recyclerview.

我正在尝试检索用户的码,以便在recyclerview中显示它们。

Here is (up to this point) my method for retrieval:

这是(到目前为止)我的检索方法:

 private List<Yard> getYards(){
        final List<Yard> list = new ArrayList<>();
        userID = mAuth.getCurrentUser().getUid();
        Log.d(TAG,"Getting to getYards()" + userID);
        FirebaseDatabase db = FirebaseDatabase.getInstance();
        DatabaseReference dbRef = db.getReference().child(getString(R.string.dbname_yards));
        Query query = dbRef.child(userID);
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    for (DataSnapshot ds: dataSnapshot.getChildren()){
                        Yard y = new Yard();
                        y = dataSnapshot.getValue(Yard.class);
                        list.add(y);
                        Log.e(TAG,""+y.toString());
                    }
                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

       return list;
    }

I have commented out the recyclerview layout manager and adapter because I just can't seem to get the data as my Yard Object.

我已经注释了recyclerview布局管理器和适配器,因为我似乎无法将数据作为我的Yard对象。

The errors I am currently receiving are:

我目前收到的错误是:

12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog W/ClassMapper: No setter/field for yard2 found on class com.bkbeesites.hivelog.Models.Yard
12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog W/ClassMapper: No setter/field for yard1 found on class com.bkbeesites.hivelog.Models.Yard
12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog E/YardsFragment: Yard{id='null', location='null', hives=0, last_visit='null', photo_url='null', yard_type='null'}
12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog W/ClassMapper: No setter/field for yard2 found on class com.bkbeesites.hivelog.Models.Yard
12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog W/ClassMapper: No setter/field for yard1 found on class com.bkbeesites.hivelog.Models.Yard
12-28 15:29:24.109 4984-4984/com.bkbeesites.hivelog E/YardsFragment: Yard{id='null', location='null', hives=0, last_visit='null', photo_url='null', yard_type='null'}

This is my data structure. I manually added these yards and also would like to know if when I am at the point where the user is adding yards, do I need to generate a key as the names?

这是我的数据结构。我手动添加了这些码并且还想知道当我在用户添加码的时候,是否需要生成一个密钥作为名称?

Sorry if this was an annoying newbie question, but I'm stumped. Thanks, in advance, for any help.

对不起,如果这是一个讨厌的新手问题,但我很难过。在此先感谢您的帮助。

1 个解决方案

#1


0  

You're using dataSnapshot instead of ds. That is, you're trying to create a yard from the uid ref instead of the individual yard[#] refs when iterating over the uid ref's children. So it should be

您正在使用dataSnapshot而不是ds。也就是说,当你在uid ref的孩子们身上进行迭代时,你正试图用uid ref创建一个院子而不是单独的院子[#] refs。所以它应该是

Yard y = ds.getValue(Yard.class);

PS: foo_bar is ugly. ???? The standard is to use fooBar.

PS:foo_bar很难看。 ????标准是使用fooBar。

#1


0  

You're using dataSnapshot instead of ds. That is, you're trying to create a yard from the uid ref instead of the individual yard[#] refs when iterating over the uid ref's children. So it should be

您正在使用dataSnapshot而不是ds。也就是说,当你在uid ref的孩子们身上进行迭代时,你正试图用uid ref创建一个院子而不是单独的院子[#] refs。所以它应该是

Yard y = ds.getValue(Yard.class);

PS: foo_bar is ugly. ???? The standard is to use fooBar.

PS:foo_bar很难看。 ????标准是使用fooBar。