kre*_*ndo 5 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
}
}
},
Run Code Online (Sandbox Code Playgroud)
我使用这个答案 How torepresentednesteddatainFirebaseclass 来创建pojo来检索嵌套数据,并且这些类正在部分工作。
我遇到的问题是答案没有解释如何从数据库检索数组数据。(不是对象列表,只是带有整数的普通数组数据)child2 pojo 没有填充来自 firebase 的数据。
hostData pojo 已成功映射到 firebase 子节点。hostData 的所有属性都是字符串,因此我认为这使得匹配很容易。
metaData 有一个数组作为它的第一个属性。
我知道 firebase 将数组存储为 json 对象,如此处所述。我需要将这些“数组”对象转换为 java 列表或 ArrayList,以便填充元数据的 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
看起来像是一个小错字问题。数据库中的键应与 POJO 类中的键匹配(区分大小写)。
public class Board {
private HostData hostData;
private MetaData metadata; // change to this
... constructor, getter, setter
}
Run Code Online (Sandbox Code Playgroud)
或这个
public class Board {
public HostData hostData;
@PropertyName("metadata")
public MetaData metaData; // make sure the field is in public
... constructor, getter, setter
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2137 次 |
| 最近记录: |