【问题标题】:DatabaseException when iterating nested Firebase database nodes迭代嵌套的 Firebase 数据库节点时出现 DatabaseException
【发布时间】:2017-01-23 19:44:58
【问题描述】:

我正在尝试迭代具有以下数据快照的嵌套 Firebase 数据库:

 DataSnapshot { key = Presidents, value = {1={aspirantName=Uh Ky, thumbnail=434, currentVotes=324595}, position=1, 2={aspirantName=Rao O, thumbnail=3, currentVotes=32}, numberofAspirants=3, docketName=Presidents} }

但是得到这个异常:

com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.esir.jualeader.Aspirant 类型

数据快照是来自

的日志
private void getAspirantsData(DataSnapshot dataSnapshot) {
    aspirantsArrayList.clear(){
    Log.e("Ds", "" + dataSnapshot); //Prints  DataSnapshot { key = Presidents, value = {1={aspirantName=Uh Ky, thumbnail=434, currentVotes=324595}, position=1, 2={aspirantName=Rao O, thumbnail=3, currentVotes=32}, numberofAspirants=3, docketName=Presidents} }
    Iterable<DataSnapshot>iterable=dataSnapshot.getChildren();
    Iterator<DataSnapshot>iterator=iterable.iterator();
    while (iterator.hasNext()){
Aspirant aspirant=iterator.next().getValue(Aspirant.class); // com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.esir.jualeader.Aspirant
        Log.e("Aspirant ", "" + aspirant.getAspirantName()+" "+aspirant.getCurrentVotes()+" "+aspirant.getThumbnail());//This Works therefore Aspirant Object has Been Created
        aspirantsArrayList.add(aspirant);
    }
}

任何人都可以就异常发生的原因提供帮助吗?它也尝试将哪个字符串转换为我的 Aspirant 对象。

这是我的应聘者课程

public class Aspirant {
    private String aspirantName;
    private int currentVotes;
    private int thumbnail;

    public Aspirant(){

    }
    public Aspirant(String aspirantName, int currentVotes, int thumbnail){
        this.aspirantName=aspirantName;
        this.currentVotes=currentVotes;
        this.thumbnail=thumbnail;
    }

    public String getAspirantName() {
        return aspirantName;
    }

    public void setAspirantName(String aspirantName) {
        this.aspirantName = aspirantName;
    }

    public int getCurrentVotes() {
        return currentVotes;
    }

    public void setCurrentVotes(int currentVotes) {
        this.currentVotes = currentVotes;
    }

    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }
}

以下是我的 Firebase 数据库结构

{
    "Presidents" : {
      "1" : {
        "aspirantName" : "Urhu atta",
        "currentVotes" : 324595,
        "thumbnail" : 434
      },
      "2" : {
        "aspirantName" : "Rla Oga",
        "currentVotes" : 32,
        "thumbnail" : 3
      },
      "docketName" : "Presidents",
      "numberofAspirants" : 3,
      "position" : 1
    },
    "Senetors" : {
      "docketName" : "Senetors",
      "numberofAspirants" : 5,
      "position" : 2
    }
}

【问题讨论】:

  • 您可能想分享您的 Aspirant 课程,这可能是问题的原因。
  • ` public class Aspirant { private String aspirantName;私人 int currentVotes;私有字符串缩略图; public Aspirant(){ } public Aspirant(String aspirantName, int currentVotes, String thumbnail){ this.aspirantName=aspirantName; this.currentVotes=currentVotes; this.thumbnail=缩略图; }}` 由 Android Studio 生成的所有其他 getter 和 setter
  • @EsirKings 您应该编辑您的问题并将课程代码添加到您的问题中。一行代码很难看懂。

标签: android firebase firebase-realtime-database


【解决方案1】:

您的 JSON 中有不兼容的数据。在President 下,您有两个有效的Aspirant 对象:

  "1" : {
    "aspirantName" : "Urhu atta",
    "currentVotes" : 324595,
    "thumbnail" : 434
  },

  "2" : {
    "aspirantName" : "Rla Oga",
    "currentVotes" : 32,
    "thumbnail" : 3
  },

但是你也有这些属性,没有一个可以转换成Aspirant

  "docketName" : "Presidents",
  "numberofAspirants" : 3,
  "position" : 1

这是 Firebase 文档建议不要将不同类型的数据嵌套在一个公共根目录下的众多原因之一。更好的数据结构是:

"Dockets": {
    "Presidents" : {
      "docketName" : "Presidents",
      "numberofAspirants" : 3,
      "position" : 1
    },
    "Senetors" : {
      "docketName" : "Senetors",
      "numberofAspirants" : 5,
      "position" : 2
    }
},
"Candidates": {
    "Presidents" : {
      "1" : {
        "aspirantName" : "Urhu atta",
        "currentVotes" : 324595,
        "thumbnail" : 434
      },
      "2" : {
        "aspirantName" : "Rla Oga",
        "currentVotes" : 32,
        "thumbnail" : 3
      }
    },
    "Senetors" : {
      ... nothing here yet
    }
}

现在您可以从Candidates 键下安全地读取所有Aspirant 对象。

【讨论】:

  • 谢谢。我想我会使用一个节点来存储有志者的数量,同时存储有志者本身。我已经按照您的建议完成了,而且效果很好。
【解决方案2】:

尝试用这个替换你的整个“getAspirantsData”函数:

private void getAspirantsData(DataSnapshot dataSnapshot) 
{
aspirantsArrayList.clear()
Log.e("Ds", "" + dataSnapshot);
for(DataSnapshot snap : datasnapshot.getChildren()
    aspirantsArrayList.add(snap.getValue(Aspirant.class);
}

【讨论】:

  • 还是不行。我猜是因为我的数据快照“docketName”中的三个不可迭代字段:“Presidents”、“numberofAspirants”:3、“position”:1`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多