【问题标题】:Need help converting Firebase data to Java Object for use in recyclerview需要帮助将 Firebase 数据转换为 Java 对象以在 recyclerview 中使用
【发布时间】:2017-12-28 20:58:51
【问题描述】:

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

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 + '\'' +
                '}';
    }
}

我正在尝试检索用户的码以在回收站视图中显示它们。

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

 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;
    }

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

我目前收到的错误是:

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?

对不起,如果这是一个烦人的新手问题,但我很难过。提前感谢您的帮助。

【问题讨论】:

标签: java android firebase firebase-realtime-database


【解决方案1】:

您使用的是dataSnapshot,而不是ds。也就是说,当迭代uid ref 的子代时,您正试图从uid ref 而非单个yard[#] refs 创建一个院子。所以应该是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多