我正在尝试将所有数据从实时数据库获取到模型图,但是我无法完全实现该方法,这是一些代码:
这是我试图获取数据的片段的一部分:
class FileListFrag : Fragment() {
companion object {
fun newInstance() = FileListFrag()
}
private lateinit var binding: FilelistFragmentBinding
private lateinit var userReference: DatabaseReference
private var dataMap: Map<String, FileData> = mutableMapOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onStart() {
super.onStart()
userReference = FirebaseDatabase.getInstance().reference.child("users").child(FirebaseAuth.getInstance().uid!!)
val userListener = object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
dataMap = p0.getValue(FileData::class.java)
}
}
userReference.addValueEventListener(userListener)
}
您可以看到我有想要将数据放入其中的地图。
private var dataMap: Map<String, FileData> = mutableMapOf()
,并在onStart()
中实施ValueEventListener
override fun onStart() {
super.onStart()
userReference = FirebaseDatabase.getInstance().reference.child("users").child(FirebaseAuth.getInstance().uid!!)
val userListener = object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
dataMap = p0.getValue(FileData::class.java)
}
}
userReference.addValueEventListener(userListener)
}
现在,当我像这样运行代码时,会出现以下异常:
Class com.example.anikatzin.Models.FileData does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
这是我的Firebase结构的图片: https://i.imgur.com/QRLJxxx.png FileData模型:
data class FileData(var date: String, var carNum: String, var ownerName: String, var carType: String, var checkListResults: Map<String, Boolean>)
非常感谢您的帮助:D