Мой RecyclerView
работает, когда я использую фиктивные данные, но отображает пустой результат для RecyclerView при подключении к базе данных Firebase.Пожалуйста, помогите решить эту проблему.
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
var adapter = qponAdapter(generateData())
val layoutManager = LinearLayoutManager(context)
qponRecyclerView?.layoutManager = layoutManager
qponRecyclerView?.itemAnimator = DefaultItemAnimator()
qponRecyclerView?.adapter = adapter
adapter.notifyDataSetChanged()
}
private fun generateData(): ArrayList<Company> {
var companyReg = Company(name = "qqqqq", br = "", country = "", city = "", address = "", tel = "", value = "", qponQuantity = 0, email = "", advApproved = "false", advPassword = "", startDate = "", endDate = "", conditions = "", postApproved = "false", order = 10000, authPhone = "",nodeID = "")
companyList.add(companyReg)
companyList.add(companyReg)
companyList.add(companyReg)
companyList.add(companyReg)
companyList.add(companyReg)
}
Он работает хорошо и отображает 5 записей в Recyclerview.
private fun generateData(): ArrayList<Company> {
val postListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
// Get Post object and use the values to update the UI
//val post = dataSnapshot.getValue<Post>(Post::class.java!!)
// ...
val td = dataSnapshot.value as HashMap<*, *>
for (key in td.keys){
val post = td[key] as HashMap<*, *>
val companyReg = Company(name = "", br = "", country = "", city = "", address = "", tel = "", value = "", qponQuantity = 0, email = "", advApproved = "false", advPassword = "", startDate = "", endDate = "", conditions = "", postApproved = "false", order = 10000, authPhone = "",nodeID = "")
companyReg.name = post["Name"].toString()
companyReg.address = post["Address"].toString()
companyReg.country = post["Country"].toString()
companyReg.city = post["City"].toString()
companyReg.tel = post["Tel"].toString()
companyReg.value = post["qponValue"].toString()
//companyReg.qponQuantity = qponQuantity
companyReg.email = post["Email"].toString()
companyReg.advPassword = post["advPassword"].toString()
companyReg.startDate = post["startDate"].toString()
companyReg.endDate = post["endDate"].toString()
companyReg.conditions = post["Conditions"].toString()
companyReg.postApproved = post["PostApproved"].toString()
companyReg.advApproved = post["advApproved"].toString()
//companyReg.order = post["Order"] as! Int
companyReg.authPhone = post["authPhone"].toString()
companyReg.nodeID = key.toString()
println(companyReg.nodeID)
println(companyReg.address)
companyList.add(companyReg)
}
}
override fun onCancelled(databaseError: DatabaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException())
// ...
}
}
myRef.addValueEventListener(postListener)
return companyList
}
Он подключен к базе данных Firebase, но RecyclerView отображает 0 записей.Я уверен, что он подключается к базе данных, потому что распечатывает каждую запись в консоли.