android– 带领域的Firebase.反序列化POJO类

android– 带领域的Firebase.反序列化POJO类,第1张

概述我的firebase数据库中有数据,一切正常,直到我尝试反序列化数据.错误:参数1的类型为io.realm.RealmList,得到了java.util.ArrayList这是我的代码:DatabaseReferenceroot=FirebaseDatabase.getInstance().getReferenceFromUrl("https://swing-8792d.firebasei

我的firebase数据库中有数据,一切正常,直到我尝试反序列化数据.

错误:参数1的类型为io.realm.RealmList,得到了java.util.ArrayList

这是我的代码:

 DatabaseReference root = FirebaseDatabase.getInstance().                getReferenceFromUrl("https://swing-8792d.firebaseio.com/playList");        query playListquery = root.orderByKey().equalTo(key);        playListquery.addListenerForSingleValueEvent(new ValueEventListener() {            @OverrIDe            public voID onDataChange(DataSnapshot dataSnapshot) {                for (DataSnapshot child : dataSnapshot.getChildren()) {                    Log.d("Child", child + "");                    PlayList receivedplayList = child.getValue(PlayList.class);                    PlayList playList = new PlayList();                    playList.setCreatedBy(receivedplayList.getCreatedBy());                    playList.setname(receivedplayList.getname());                    playList.setMyMap(receivedplayList.getMyMap());                    playList.setQrKey(receivedplayList.getQrKey());                    playList.setCount(receivedplayList.getCount());                    playList.setID(receivedplayList.getID());                    playList.setTracks(receivedplayList.getTracks());                    mPlayList.add(playList); }

这是我的POJO课程:

@RealmClasspublic class PlayList extends RealmObject {String name;Long ID;RealmList<Track> tracks;Integer count;String createdBy;RealmList<UserMap> myMap;String qrKey;public RealmList<UserMap> getMyMap() {    return myMap;}public voID setMyMap(RealmList<UserMap> myMap) {    this.myMap = myMap;}public PlayList(){}public String getQrKey() {    return qrKey;}public voID setQrKey(String qrKey) {    this.qrKey = qrKey;}public String getCreatedBy() {    return createdBy;}public voID setCreatedBy(String createdBy) {    this.createdBy = createdBy;}public RealmList<Track> getTracks() {    return tracks;}public voID setTracks(RealmList<Track> tracks) {    this.tracks = tracks;}public String getname() {    return name;}public voID setname(String name) {    this.name = name;}public Long getID() {    return ID;}public voID setID(Long ID) {    this.ID = ID;}public Integer getCount() {    return count;}public voID setCount(Integer count) {    this.count = count;}

}

如果我尝试使用普通POJO类(即删除领域)进行反序列化,它可以正常工作.

解决方法:

Firebase不适用于没有默认构造函数或私有变量的类,即没有公共getter / setter.

在您的情况下,一个更简单的解决方案是创建一个中间件类,它是相同的pojo而不是扩展RealmObject.接下来使用pojo的值初始化您的RealmObject子类.

伪代码

class SimplePojoPlayList  {     public String variable;}class PlayList extends RealmObject {     public String variable;}

然后首先进入SimplePojoPlayList

    for (DataSnapshot child : dataSnapshot.getChildren()) {         SimplePojoPlayList receivedplayList = child.getValue(SimplePojoPlayList.class);         PlayList playList = new PlayList();         playList.variable = receivedplayList.variable;    }
总结

以上是内存溢出为你收集整理的android – 带领域的Firebase.反序列化POJO类全部内容,希望文章能够帮你解决android – 带领域的Firebase.反序列化POJO类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存