如何使用数据模型从Firebase实时数据库中检索数据?

时间:2019-09-29 21:27:09

标签: firebase kotlin firebase-realtime-database

enter image description here 图书有多个数据 本章有多个数据 诗歌有多个数据

我的模型和Firebase数据库如下所示,我如何使用模型中的章节和经文检索我的所有数据

Class Model_bible(
 val book_array:List<Model_bible_bible_array>?=null
){}

Class Model_bible_bible_array(
 val book_array:HasMap<String,Model_bible_chapter>?=null
){}

Class Model_bible_chapter(
 val book_array:List<Model_bible_chapter_hasmap>?=null
){}

Class Model_bible_chapter_hasmap(
 val book_array:HasMap<String,Model_bible_Verse_array>?=null
){}

Class Model_Model_bible_Verse_array(
 val book_array:List<Verse_data>?=null
){}

Class Verse_data(
 val Verse:String = "",
 Val Verse_id:String = ""
){}

2 个答案:

答案 0 :(得分:1)

从Firebase中的实时数据库下载JSON(如果我没记错的话,请放在右上角)。在http://www.jsonschema2pojo.org/处粘贴JSON内容,它将生成适当的模型结构。

这是Kotlin数据类模型:

import kotlinx.serialization.Serializable

@Serializable
data class SomeTestModelClass(
    val img: List<Img?>? = null
) {
    @Serializable
    data class Img(
        val imagedata: List<Imagedata?>? = null,
        val imgdata: List<Imgdata?>? = null
    ) {
        @Serializable
        data class Imagedata(
            val `data`: List<Data?>? = null
        ) {
            @Serializable
            data class Data(
                val img: String? = null
            )
        }

        @Serializable
        data class Imgdata(
            val `data`: List<Data?>? = null
        ) {
            @Serializable
            data class Data(
                val img: String? = null
            )
        }
    }
}

它正在使用https://github.com/Kotlin/kotlinx.serialization库。

答案 1 :(得分:0)

使用以下模型获取我的所有数据

Java POJO类和Kotlin

@Serializable()
public class Example {

    @SerializedName("Book")
    @Expose
    private List<Book> book = null;

    public List<Book> getBook() {
        return book;
    }

    public void setBook(List<Book> book) {
        this.book = book;
    }

    @Serializable
    public class Book {
        @SerializedName("Chapter")
        @Expose

        private List<Chapter> chapter = null;

        public List<Chapter> getChapter() {
            return chapter;
        }

        public void setChapter(List<Chapter> chapter) {
            this.chapter = chapter;
        }

        @Serializable
        public class Chapter {

            @SerializedName("Verse")
            @Expose
            private List<Verse> verse = null;

            public List<Verse> getVerse() {
                return verse;
            }

            public void setVerse(List<Verse> verse) {
                this.verse = verse;
            }

            @Serializable
            public class Verse {

                @SerializedName("Verseid")
                @Expose
                private String verseid;
                @SerializedName("Verse")
                @Expose

                private String verse;

                public String getVerseid() {
                    return verseid;
                }

                public void setVerseid(String verseid) {
                    this.verseid = verseid;
                }

                public String getVerse() {
                    return verse;
                }

                public void setVerse(String verse) {
                    this.verse = verse;
                }

            }
        }
    }
}

下面的代码将用于通过模型获取数据


        query.addListenerForSingleValueEvent(object : ValueEventListener {

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val jsonString = gson?.toJson(dataSnapshot.value)
                val staff: Example = gson!!.fromJson(jsonString, Example::class.java)
            }

            override fun onCancelled(error: DatabaseError) {
                // Failed to read value
            }
        })

数据类和Kotlin代码

@Serializable
data class SomeTestModelClass(
    @SerializedName("Book")
    val Book: List<Img?>? = null
) {
    @Serializable
    data class Img(
        @SerializedName("Chapter")
        val Chapter: List<Imagedata?>? = null
    ) {
        @Serializable
        data class Imagedata(
            @SerializedName("Verse")
            val Verse: List<Data?>? = null
        ) {
            @Serializable
            data class Data(
                @SerializedName("Verseid")
                val Verseid: String? = null,
                @SerializedName("Verse")
                val Verse: String? = null
            )
        }

    }
}

main.kt文件

 query.addListenerForSingleValueEvent(object : ValueEventListener {

            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val jsonString = gson?.toJson(dataSnapshot.value)
                val staff: SomeTestModelClass = gson!!.fromJson(jsonString, SomeTestModelClass::class.java)
                Log.d("data",staff.toString())
            }

            override fun onCancelled(error: DatabaseError) {
                // Failed to read value
            }
        })