转换存储的Firebase"数组"反对POJO

时间:2016-11-20 09:16:30

标签: android firebase android-recyclerview firebase-realtime-database

我正在尝试从我的Android应用程序中的recyclerview中的firebase中检索数据。 " boardz"的对象我引用的节点有2个子属性。这些属性都有多个子属性。

使用Firebase json和pojo更新:

{
"game" : {
"boardz" : {
  "-KTaL6V06e3SkEAvfU_3" : {
    "hostData" : {
      "hostId" : "MUC6gtRqInRQmqxe7khlmrAbjVZ2",
      "ownerAddress" : "n3b9t3vDR6Yiot4HwxwZoa7vxZBiAKReeH",
      "secretString" : "derp-a-derp"
    },
    "metadata" : {
      "across" : [ 2, 0, 5, 9, 7, 8, 1, 4, 3, 6 ],
      "awayTeam" : "ATL",
      "boardFeeBTC" : "",
      "boardFeeUSD" : 14,
      "collectionAddress" : "2NEPnGPyvy9F5N4W4CjhLNPZLwTFNSAzjAL",
      "down" : [ 1, 6, 8, 7, 3, 5, 2, 0, 9, 4 ],
      "endTime" : "2016-10-09T22:09:39.807Z",
      "homeTeam" : "DEN",
      "q1AwayScore" : "",
      "q1HomeScore" : "",
      "q1WinnerAddress" : "",
      "q2AwayScore" : "",
      "q2HomeScore" : "",
      "q2WinnerAddress" : "",
      "q3AwayScore" : "",
      "q3HomeScore" : "",
      "q3WinnerAddress" : "",
      "q4AwayScore" : "",
      "q4HomeScore" : "",
      "q4WinnerAddress" : "",
      "startTime" : "2016-10-08T22:09:39.807Z",
      "winnersPaid" : false
    }
  },
  "-KTaP0-GPSCwCDA8BUlt" : {
    "hostData" : {
      "hostId" : "MUC6gtRqInRQmqxe7khlmrAbjVZ2",
      "ownerAddress" : "mtLF8hks46i9DbSK1tBpenYMDsHpTbnuwC",
      "secretString" : "do-diddly-doo"
    },
    "metadata" : {
      "across" : [ 9, 1, 6, 8, 0, 2, 4, 7, 3, 5 ],
      "awayTeam" : "BUF",
      "boardFeeBTC" : "",
      "boardFeeUSD" : 25,
      "collectionAddress" : "2N5VgMfKwCtJaiT4CLVAMvMQUG6PTbreY4z",
      "down" : [ 9, 2, 4, 3, 7, 0, 5, 6, 8, 1 ],
      "endTime" : "2016-10-09T22:26:47.467Z",
      "homeTeam" : "LA",
      "q1AwayScore" : "",
      "q1HomeScore" : "",
      "q1WinnerAddress" : "",
      "q2AwayScore" : "",
      "q2HomeScore" : "",
      "q2WinnerAddress" : "",
      "q3AwayScore" : "",
      "q3HomeScore" : "",
      "q3WinnerAddress" : "",
      "q4AwayScore" : "",
      "q4HomeScore" : "",
      "q4WinnerAddress" : "",
      "startTime" : "2016-10-08T22:26:47.467Z",
      "winnersPaid" : false
    }
  }
},

我用过这个答案 How to represent nested data in Firebase class 创建pojo以检索嵌套数据,并且类正在部分工作。

我遇到的问题是答案并没有解释如何从数据库中检索数组数据。 (不是对象列表,只是带有整数的普通数组数据)child2 pojo不会被来自firebase的数据填充。

hostData pojo已成功映射到firebase子节点。所有hostData的属性都是字符串,所以我觉得它很容易匹配。

metaData有一个数组作为它的第一个属性。

我知道firebase将数组存储为json对象,如here所述。我需要转换这些"数组"将对象放入java列表或ArrayList中,以便填充metaData的pojo。

那么如何将存储在firebase节点中的数组数据表示为pojo?

我添加了pojo以帮助了解我是如何创建对象并映射属性的。也许我错过了什么。

public class Board {

private HostData hostData;//this class maps to the firebase json
private MetaData metaData;//this class isn't mapping to any of the json properties.

public Board() {
}

public Board(HostData hostData, MetaData metaData) {
    this.hostData = hostData;
    this.metaData = metaData;
}

public HostData getHostData() {
    return hostData;
}

public MetaData getMetaData() {
    return metaData;
}
//the hostData class works fine
public static class HostData {

    private String hostId;
    private String ownerAddress;
    private String secretString;

    public HostData() {
    }

    public HostData(String hostId, String ownerAddress, String secretString) {
        this.hostId = hostId;
        this.ownerAddress = ownerAddress;
        this.secretString = secretString;
    }

    public String getHostId() {
        return hostId;
    }

    public String getOwnerAddress() {
        return ownerAddress;
    }

    public String getSecretString() {
        return secretString;
    }
}
//this class is giving me hell. Am I missing something?
public static class MetaData {

    private List<Long> across = new ArrayList<>();
    private String awayTeam;
    private Double boardFeeBTC;
    private Double boardFeeUSD;
    private String collectionAddress;
    private List<Long> down = new ArrayList<>();
    private String endTime;
    private String homeTeam;
    private Long q1AwayScore;
    private Long q1HomeScore;
    private String q1WinnerAddress;
    private Long q2AwayScore;
    private Long q2HomeScore;
    private String q2WinnerAddress;
    private Long q3AwayScore;
    private Long q3HomeScore;
    private String q3WinnerAddress;
    private Long q4AwayScore;
    private Long q4HomeScore;
    private String q4WinnerAddress;
    private String startTime;
    private Boolean winnersPaid;

    public MetaData() {
    }

    public MetaData(List<Long> across, String awayTeam, Double boardFeeBTC, Double boardFeeUSD, String collectionAddress, List<Long> down, String endTime, String homeTeam, Long q1AwayScore, Long q1HomeScore, String q1WinnerAddress, Long q2AwayScore, Long q2HomeScore, String q2WinnerAddress, Long q3AwayScore, Long q3HomeScore, String q3WinnerAddress, Long q4AwayScore, Long q4HomeScore, String q4WinnerAddress, String startTime, Boolean winnersPaid) {
        this.across = across;
        this.awayTeam = awayTeam;
        this.boardFeeBTC = boardFeeBTC;
        this.boardFeeUSD = boardFeeUSD;
        this.collectionAddress = collectionAddress;
        this.down = down;
        this.endTime = endTime;
        this.homeTeam = homeTeam;
        this.q1AwayScore = q1AwayScore;
        this.q1HomeScore = q1HomeScore;
        this.q1WinnerAddress = q1WinnerAddress;
        this.q2AwayScore = q2AwayScore;
        this.q2HomeScore = q2HomeScore;
        this.q2WinnerAddress = q2WinnerAddress;
        this.q3AwayScore = q3AwayScore;
        this.q3HomeScore = q3HomeScore;
        this.q3WinnerAddress = q3WinnerAddress;
        this.q4AwayScore = q4AwayScore;
        this.q4HomeScore = q4HomeScore;
        this.q4WinnerAddress = q4WinnerAddress;
        this.startTime = startTime;
        this.winnersPaid = winnersPaid;
    }

    public List<Long> getAcross() {
        return across;
    }

    public String getAwayTeam() {
        return awayTeam;
    }

    public Double getBoardFeeBTC() {
        return boardFeeBTC;
    }

    public Double getBoardFeeUSD() {
        return boardFeeUSD;
    }

    public String getCollectionAddress() {
        return collectionAddress;
    }

    public List<Long> getDown() {
        return down;
    }

    public String getEndTime() {
        return endTime;
    }

    public String getHomeTeam() {
        return homeTeam;
    }

    public Long getQ1AwayScore() {
        return q1AwayScore;
    }

    public Long getQ1HomeScore() {
        return q1HomeScore;
    }

    public String getQ1WinnerAddress() {
        return q1WinnerAddress;
    }

    public Long getQ2AwayScore() {
        return q2AwayScore;
    }

    public Long getQ2HomeScore() {
        return q2HomeScore;
    }

    public String getQ2WinnerAddress() {
        return q2WinnerAddress;
    }

    public Long getQ3AwayScore() {
        return q3AwayScore;
    }

    public Long getQ3HomeScore() {
        return q3HomeScore;
    }

    public String getQ3WinnerAddress() {
        return q3WinnerAddress;
    }

    public Long getQ4AwayScore() {
        return q4AwayScore;
    }

    public Long getQ4HomeScore() {
        return q4HomeScore;
    }

    public String getQ4WinnerAddress() {
        return q4WinnerAddress;
    }

    public String getStartTime() {
        return startTime;
    }

    public Boolean getWinnersPaid() {
        return winnersPaid;
    }
}
}

2 个答案:

答案 0 :(得分:3)

看起来像一个小错字问题。数据库中的键应与(区分大小写)匹配POJO类中的键。

public class Board {
    private HostData hostData;
    private MetaData metadata; // change to this

    ... constructor, getter, setter
}

或者

public class Board {
    public HostData hostData;
    @PropertyName("metadata")
    public MetaData metaData; // make sure the field is in public

    ... constructor, getter, setter
}

答案 1 :(得分:2)

转到设置 - &gt;插件 - &gt;浏览存储库 - &gt; Serch RoboPOJOgenerator 。并关注this文档。