W / ClassMapper:在Java中使用FirebaseListAdapter时没有setter / field

时间:2016-12-22 23:22:27

标签: android firebase firebase-realtime-database

我正在尝试构建一个简单的聊天应用。我是Firebase的新手。数据库的结构如下:

enter image description here

这是我的代码:

databaseReference = FirebaseDatabase.getInstance().getReference().child("Chat");
    firebaseListAdapter = new FirebaseListAdapter<Room>(this, Room.class, android.R.layout.simple_list_item_1, databaseReference) {
        @Override
        protected void populateView(View v, Room room, int position) {
            TextView textView = (TextView) v.findViewById(android.R.id.text1);
            String roomName = this.getRef(position).getKey();
            textView.setText(roomName);
        }
    };
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(firebaseListAdapter);

这是我的Room课程:

    class Room {
    private String room;

    public Room() {}

    public Room(String room) {this.room = room;}

    public void setRoom(String room) {this.room = room;}
    public String getRoom() {return room;}
}

当我尝试填充列表视图时,项目会正确显示,但是虽然我已设置了getter和setter,但每次启动活动时,都会出现12次错误:

W/ClassMapper: No setter/field for -KZcHa8wYpcvLSb7atX- found on class example.com.chat.Room

我该如何解决这个问题?提前谢谢!

1 个答案:

答案 0 :(得分:1)

使用该数据库结构,您无法摆脱这些警告,因为FirebaseListAdapter会自动尝试将响应从firebase解析为对象类。

您有两个选择:

  • 更改数据库结构
  • 请勿使用FirebaseListAdapter,而是使用ChildEventListener,然后手动将值添加到ListView

如果要更改数据库结构,可以将其更改为

 {
    "Chat": {
        "Room1": {
            ...
        },
        "Room2": {
            ...
        },
        "Room3": {
            ...
        }
    },
    "Chat_Rooms": {
        "Room1": true,
        "Room2": true,
        "Room3": true
    }
}

然后将DatabaseReference引用为“Chat_Rooms”

databaseReference = FirebaseDatabase.getInstance().getReference().child("Chat_Rooms");
firebaseListAdapter = new FirebaseListAdapter<Boolean>(this, Boolean.class, android.R.layout.simple_list_item_1, databaseReference) {
    @Override
    protected void populateView(View v, Boolean value, int position) {
        TextView textView = (TextView) v.findViewById(android.R.id.text1);
        String roomName = this.getRef(position).getKey();
        textView.setText(roomName);
    }
};