Android 从Firestore获取对象数组

Android 从Firestore获取对象数组,android,firebase,dictionary,arraylist,kotlin,Android,Firebase,Dictionary,Arraylist,Kotlin,我从firestore获取数据并将其放入ArrayList arraylist=document.get("questionsList") as ArrayList<Question> Toast.makeText(context, arraylist.size, Toast.LENGTH_LONG).show() 结果是 java.util.HashMap不能转换为问题 问题课 class Question (var question:String,var choices:Ar

我从firestore获取数据并将其放入ArrayList

arraylist=document.get("questionsList") as ArrayList<Question>
Toast.makeText(context, arraylist.size, Toast.LENGTH_LONG).show()
结果是 java.util.HashMap不能转换为问题

问题课

class Question (var question:String,var choices:ArrayList<String>,var correctAnswer:String
                ,private var userAnswer:String): Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString()!!, arrayListOf<String>().apply {
            parcel.readString()
        },
        parcel.readString()!!,
        parcel.readString()!!
    )
    constructor():this(question="",choices = ArrayList<String>(),correctAnswer = "",userAnswer = "")

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(question)
        parcel.writeString(correctAnswer)
        parcel.writeString(userAnswer)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Question> {
        override fun createFromParcel(parcel: Parcel): Question {
            return Question(parcel)
        }

        override fun newArray(size: Int): Array<Question?> {
            return arrayOfNulls(size)
        }
    }
}
类问题(变量问题:字符串,变量选项:ArrayList,变量回答:字符串
,private var userAnswer:String):可包裹{
构造函数(地块:地块):此(
parcel.readString()!!,arrayListOf().apply{
parcel.readString()
},
parcel.readString()!!,
parcel.readString()!!
)
构造函数():此(问题=”,选项=ArrayList(),更正答案=”,用户答案=)
覆盖地块(地块:地块,标志:Int){
包裹书面记录(问题)
包裹书写(正确答案)
parcel.writeString(用户应答)
}
覆盖有趣的描述内容():Int{
返回0
}
同伴对象创建者:Parcelable.CREATOR{
覆盖createFromParcel(地块:地块):问题{
退货问题(包裹)
}
覆盖新数组(大小:Int):数组{
返回阵列fnulls(大小)
}
}
}

您可以使用
document.toObject
将Firestore结果强制转换为Kotlin类。如果您只在字段上使用
get
,您将得到一个HashMap。在您的例子中,您可以创建一个具有
questionsList
属性的类,然后将其强制转换到您的类。我已经几个月没有使用Kotlin了,但我相信它会是这样的:

data class MyQuestionList(
    var questionsList: ArrayList<Question>
)

val myQuestionList = document.toObject(MyQuestionList::class.java)

Toast.makeText(context, myQuestionList.questionsList!![0].question, Toast.LENGTH_LONG).show()
数据类MyQuestionList(
变量问题列表:ArrayList
)
val myQuestionList=document.toObject(myQuestionList::class.java)
Toast.makeText(上下文,myQuestionList.questionsList!![0]。问题,Toast.LENGTH\u LONG.show()

另外,要小心使用
,因为如果对象为null,它将导致运行时异常。

您可以使用
document.toObject
将Firestore结果强制转换为Kotlin类。如果您只在字段上使用
get
,您将得到一个HashMap。在您的例子中,您可以创建一个具有
questionsList
属性的类,然后将其强制转换到您的类。我已经几个月没有使用Kotlin了,但我相信它会是这样的:

data class MyQuestionList(
    var questionsList: ArrayList<Question>
)

val myQuestionList = document.toObject(MyQuestionList::class.java)

Toast.makeText(context, myQuestionList.questionsList!![0].question, Toast.LENGTH_LONG).show()
数据类MyQuestionList(
变量问题列表:ArrayList
)
val myQuestionList=document.toObject(myQuestionList::class.java)
Toast.makeText(上下文,myQuestionList.questionsList!![0]。问题,Toast.LENGTH\u LONG.show()

另外,要小心使用
因为如果对象为空,它将导致运行时异常。

请添加数据库结构以查看
问题列表
属性,并用@AlexMamo@AlexMamo我添加了它。请同时添加
问题
类的内容。@AlexMamo请添加数据库结构以查看您的
问题列出
属性,请用@AlexMamo@AlexMamo我添加了它。请同时添加你的
问题
类的内容。@AlexMamo它在上面