Firebase数据快照值未反映在关联的类对象中

时间:2017-10-04 09:45:32

标签: android firebase firebase-realtime-database

我是Firebase的新手。我正在尝试从firebase获取数据到相关类的对象。数据快照从Firebase获取值,但不会将相同的值分配给对象。

以下是我的Firebase结构:

enter image description here

以下是解决问题的方法:

 myRef =database.getReference();






    myRef.child("Tables").addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded( DataSnapshot dataSnapshot, String s) {
            Log.v("DS",dataSnapshot.getValue().toString());
            Tables values=dataSnapshot.getValue(Tables.class);

            Log.v("isAl", values.toString());


            int Capacity=values.getCapacity();
            String Customer=values.getCustomer();
            boolean IsAllocated= values.isAllocated();

            Log.v("isAl", String.valueOf(values.isAllocated())+"\t"+Customer+"\t"+Capacity);



            String key = dataSnapshot.getKey();
            oldKey=key;

            mKeys.add(key);

            Tables table=new Tables(Capacity,Customer,IsAllocated);
});
}

我的桌子课程如下:

public class Tables {
    private int Capacity;
    private   String Customer;
    private   boolean IsAllocated;

public Tables(int capacity, String customer, boolean isAllocated) {
    Capacity = capacity;
    Customer = customer;
    IsAllocated = isAllocated;
}

public Tables() {
}

public int getCapacity() {
    return Capacity;
}

public void setCapacity(int capacity) {
    Capacity = capacity;
}

public String getCustomer() {
    return Customer;
}

public void setCustomer(String customer) {
    Customer = customer;
}

public boolean isAllocated() {
    return IsAllocated;
}

public void setAllocated(boolean allocated) {
    IsAllocated = allocated;
}
}

这是我的适配器类的BindViewHolder:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        list=Tlist.get(position);
        Log.v("vlist", String.valueOf(Tlist.get(position)));

        holder.tabletext.setText("Table");
        holder.status.setText(String.valueOf(list.isAllocated()));


    }

在values对象中,我得到空值。任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:2)

更改您使用区分大小写的类变量,您使用的是小字符,而Firebase数据库包含大写

将您的班级更改为:

public class Tables {
   int Capacity;
   String Customer;
   Boolean IsAllocated;

并使用ValueEventListener获取单个

中的所有记录
myRef.child("Tables").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        try {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Tables values=dataSnapshot.getValue(Tables.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

修改1:您可以使用地图下面的代码尝试获取价值

myRef.child("Tables").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        try {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Map<String,String> map=(Map<String,String>)postSnapshot.getValue();
                String Customer=map.get("Customer");
                String IsAllocated= map.get("IsAllocated");
                String Capacity= map.get("Capacity");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});