处理包含多个列表和其他原语的对象(反序列化时预期一个List,但得到一个类java.util.HashMap)

时间:2018-10-18 21:23:23

标签: android firebase arraylist hashmap generic-type-argument

我的数据结构 enter image description here

我的对象有多个ArrayList和基元。 检索时,反序列化时出现此错误Expected List,但得到了一个类java.util.HashMap。

我知道我收到此错误,因为Firebase就是这样存储它的。 我只是从未在这些领域工作过。

我的问题是可以直接将其转换为对象吗?  malhobayyeb在这里https://stackoverflow.com/a/35979715/9860553指出了这一点,这似乎与我的情况非常接近,但他建议的答案无效。

我也在附加我的Objects结构。

尽管我认为不需要代码。 问题只是“在firebase中,如果列表在另一个对象内,如何处理数据”

package com.example.desktop.digitlbc.Model;

import java.util.ArrayList;

public class BusinessData {
private String
        tvbAbout,
        tvbProduct,tvbNews,tvbTrends,tvbOffers,
        tvbEvents,
        tvbCarrers;

private ArrayList<ImageData> idAlImageDataGallery;
private ArrayList<ImageData> idAlImageDataDocGallery;
private ArrayList<ImageData> idAlImageDataPersGallery;
private ArrayList<ImageData> idAlImageDataBizGallery;

private ArrayList<ImageData> idAlProduct;
private ArrayList<ImageData> idAlNews;
private ArrayList<ImageData> idAlTrends;
private ArrayList<ImageData> idAlOffers;

private String id;
private long timestamp;

private ArrayList<Product> product;

public BusinessData() {
}

public BusinessData(String tvbAbout, String tvbProduct, String tvbNews, String tvbTrends, String tvbOffers, String tvbEvents, String tvbCarrers, ArrayList<ImageData> idAlImageDataGallery, ArrayList<ImageData> idAlImageDataDocGallery, ArrayList<ImageData> idAlImageDataPersGallery, ArrayList<ImageData> idAlImageDataBizGallery, ArrayList<ImageData> idAlProduct, ArrayList<ImageData> idAlNews, ArrayList<ImageData> idAlTrends, ArrayList<ImageData> idAlOffers, String id, long timestamp, ArrayList<Product> product) {
    this.tvbAbout = tvbAbout;
    this.tvbProduct = tvbProduct;
    this.tvbNews = tvbNews;
    this.tvbTrends = tvbTrends;
    this.tvbOffers = tvbOffers;
    this.tvbEvents = tvbEvents;
    this.tvbCarrers = tvbCarrers;
    this.idAlImageDataGallery = idAlImageDataGallery;
    this.idAlImageDataDocGallery = idAlImageDataDocGallery;
    this.idAlImageDataPersGallery = idAlImageDataPersGallery;
    this.idAlImageDataBizGallery = idAlImageDataBizGallery;
    this.idAlProduct = idAlProduct;
    this.idAlNews = idAlNews;
    this.idAlTrends = idAlTrends;
    this.idAlOffers = idAlOffers;
    this.id = id;
    this.timestamp = timestamp;
    this.product = product;
}

public ArrayList<Product> getProduct() {
    return product;
}

public void setProduct(ArrayList<Product> product) {
    this.product = product;
}

public String getTvbAbout() {
    return tvbAbout;
}

public void setTvbAbout(String tvbAbout) {
    this.tvbAbout = tvbAbout;
}

public String getTvbProduct() {
    return tvbProduct;
}

public void setTvbProduct(String tvbProduct) {
    this.tvbProduct = tvbProduct;
}

public String getTvbNews() {
    return tvbNews;
}

public void setTvbNews(String tvbNews) {
    this.tvbNews = tvbNews;
}

public String getTvbTrends() {
    return tvbTrends;
}

public void setTvbTrends(String tvbTrends) {
    this.tvbTrends = tvbTrends;
}

public String getTvbOffers() {
    return tvbOffers;
}

public void setTvbOffers(String tvbOffers) {
    this.tvbOffers = tvbOffers;
}

public String getTvbEvents() {
    return tvbEvents;
}

public void setTvbEvents(String tvbEvents) {
    this.tvbEvents = tvbEvents;
}

public String getTvbCarrers() {
    return tvbCarrers;
}

public void setTvbCarrers(String tvbCarrers) {
    this.tvbCarrers = tvbCarrers;
}

public ArrayList<ImageData> getIdAlImageDataGallery() {
    return idAlImageDataGallery;
}

public void setIdAlImageDataGallery(ArrayList<ImageData> idAlImageDataGallery) {
    this.idAlImageDataGallery = idAlImageDataGallery;
}

public ArrayList<ImageData> getIdAlImageDataDocGallery() {
    return idAlImageDataDocGallery;
}

public void setIdAlImageDataDocGallery(ArrayList<ImageData> idAlImageDataDocGallery) {
    this.idAlImageDataDocGallery = idAlImageDataDocGallery;
}

public ArrayList<ImageData> getIdAlImageDataPersGallery() {
    return idAlImageDataPersGallery;
}

public void setIdAlImageDataPersGallery(ArrayList<ImageData> idAlImageDataPersGallery) {
    this.idAlImageDataPersGallery = idAlImageDataPersGallery;
}

public ArrayList<ImageData> getIdAlImageDataBizGallery() {
    return idAlImageDataBizGallery;
}

public void setIdAlImageDataBizGallery(ArrayList<ImageData> idAlImageDataBizGallery) {
    this.idAlImageDataBizGallery = idAlImageDataBizGallery;
}

public ArrayList<ImageData> getIdAlProduct() {
    return idAlProduct;
}

public void setIdAlProduct(ArrayList<ImageData> idAlProduct) {
    this.idAlProduct = idAlProduct;
}

public ArrayList<ImageData> getIdAlNews() {
    return idAlNews;
}

public void setIdAlNews(ArrayList<ImageData> idAlNews) {
    this.idAlNews = idAlNews;
}

public ArrayList<ImageData> getIdAlTrends() {
    return idAlTrends;
}

public void setIdAlTrends(ArrayList<ImageData> idAlTrends) {
    this.idAlTrends = idAlTrends;
}

public ArrayList<ImageData> getIdAlOffers() {
    return idAlOffers;
}

public void setIdAlOffers(ArrayList<ImageData> idAlOffers) {
    this.idAlOffers = idAlOffers;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
}
}

编辑1

现在更改了代码。使用上面的链接中的答案,我已经以非常穴居的方式实现了我现在想要的。但这使代码对将来的更新毫无用处。

我的原始代码是一个简单的valueEventListener。没什么特别的。一点都没有。

businessData = dataSnapshot.getValue(BusinessData.class);

裁判也指向正确的位置

mRef = database.getReference(Common.id).child("bizData");

0 个答案:

没有答案