从Firebase读取数据并将其保存在Array列表中

时间:2017-10-31 14:53:15

标签: java android firebase firebase-realtime-database

当我尝试从数据库中检索数据并将其保存到数组列表中时,我收到以下错误:com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap

@Override
    public List<Player> getPlayers(String gameId) {
        final List<Player> commentKeys = new ArrayList<>();
        DatabaseReference nishi = FirebaseDatabase.getInstance().getReference("players");
        Query query = nishi.orderByChild("game_id").equalTo(gameId);
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot childSnapshot : snapshot.getChildren()) {
                    commentKeys.add((Player) childSnapshot.getValue());
                }
                Log.d("Data:", commentKeys.toString());

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return commentKeys;
    }

这是我的Player课程:

public class Player {

    private String playerId;
    private String name;
    private String color;
    private int points;
    private String gameId;

    public Player() {
    }

    public Player(String id, String name, String color, int points, String gameId) {
        this.playerId = id;
        this.name = name;
        this.color = color;
        this.points = points;
        this.gameId = gameId;
    }

    public String getPlayerId() {
        return playerId;
    }

    public void setPlayerId(String playerId) {
        this.playerId = playerId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

    public String getGameId() {
        return gameId;
    }

    public void setGameId(String gameId) {
        this.gameId = gameId;
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

这意味着即使您将childSnapshot.getValue()投射到PlayerPlayer也不会返回Map类的对象。由于Firebase数据库的结构是键和值对,这意味着Firebase数据库的每个节点都是Player,在该特定位置,您没有获得java.util.HashMap类的对象,获得Player。您似乎只是在JSON树中以错误的级别读取数据。解决方案是将您的侦听器附加到树中的正确级别,该级别应该与List<Player> commentKeys = new ArrayList<>();类完全相同。

不要忘记在onDataChange()方法中添加password = os.getenv('ENVNAME')的缩写,否则它将始终为空。

答案 1 :(得分:0)

尝试澄清childSnapshot.getValue:

的数据类型
FIND PSESCDel EXCLUSIVE-LOCK
             WHERE PSESCDel.EmployeeCode    = PSESC.EmployeeCode
               AND PSESCDel.ServiceCoverRef = PSESC.ServiceCoverRef NO-ERROR.


        IF NOT AVAILABLE PSESCDel THEN
        DO:
            IF PSESC.EmployeeCode = "0358" THEN
                    MESSAGE "RM NOT AV" PSESC.ServiceCoverRef.

            CREATE PSESCDel.
            ASSIGN PSESCDel.EmployeeCode    = PSESC.EmployeeCode
                   PSESCDel.ServiceCoverRef = PSESC.ServiceCoverRef
                   PSESCDel.DeletedDate     = TODAY.

            IF PSESC.EmployeeCode = "0350" THEN
            DO:
                    MESSAGE "RM IT CAME TO CREATE".
                    IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES > 0 THEN DO:
                            MESSAGE "RM ERROR NUMBER" STRING(ERROR-STATUS:GET-NUMBER(1)).
                            MESSAGE "RM ERROR MESSAGE" ERROR-STATUS:GET-MESSAGE(1).
                    END.
            END.



        END.

如果它获得了HashMap,那么你应该改变你的:

 for (DataSnapshot childSnapshot : snapshot.getChildren()) {
     commentKeys.add((Player) childSnapshot.getValue());
 }

我希望它会有所帮助。