Hardik raiyani
Hardik raiyani

Reputation: 114

How can i retrive data from firebase realtime database using data model?

enter image description here Book has multiple data Chapter has multiple data Verse has Multiple data

my model and firebase data base given as below how can i retrive my all data with chapter and verse wise in model

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 = ""
){}

Upvotes: 0

Views: 360

Answers (2)

Hardik raiyani
Hardik raiyani

Reputation: 114

get my all data using below model

Java POJO class and 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;
                }

            }
        }
    }
}

below code will be use for get data with model


        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
            }
        })

Data class and kotlin code

@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 file

 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
            }
        })

Upvotes: 0

Simon
Simon

Reputation: 1737

Download the JSON from the realtime db in Firebase (right top corner if I'm not mistaken). Paste the JSON content here http://www.jsonschema2pojo.org/ and it will generate the appropriate model structure.

Here is the Kotlin data class model:

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
            )
        }
    }
}

It's using https://github.com/Kotlin/kotlinx.serialization library.

Upvotes: 1

Related Questions