我有一个片段,里面有两个 recyclerviews
。一个显示拾取列表,另一个显示缺席列表。当数据库中发生任何更改时,拾取列表可以完美运行并立即更新。但是,在进行更改时,缺席列表不会立即更新。如果我在数据库中进行更改,然后切换到不同的片段,然后返回,我会看到更改。我似乎无法弄清楚为什么选择列表有效,而缺席列表无效。
片段代码
override fun onStart() {
super.onStart()
//Query for Pickup Request
var pickupQuery =
db.collection("Users").whereNotEqualTo("pickupTime", "").orderBy("pickupTime")
// RecyclerOptions for Pickup Request
var pickupOptions = pickupFirestoreOptions(pickupQuery)
pickupAdapter = PickupAdapter(pickupOptions)
binding.rvPickupList.adapter = pickupAdapter
binding.rvPickupList.layoutManager =
LinearLayoutManager(requireContext())
// Query for Absent Request
val today = LocalDateTime.now()
val formattedToday = "${today.monthValue}-${today.dayOfMonth}-${today.year}"
var absentQuery = db.collection("Users").whereArrayContains("absentDates", formattedToday)
.orderBy("studentFirstName")
// RecyclerOptions for Absent Request
var absentOptions = absentFirestoreOptions(absentQuery)
absentAdapter = AbsentAdapter(absentOptions)
binding.rvAbsentList.adapter = absentAdapter
binding.rvAbsentList.layoutManager =
GridLayoutManager(requireContext(), 4)
pickupAdapter.startListening()
absentAdapter.startListening()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
pickupAdapter.stopListening()
absentAdapter.stopListening()
}
private fun pickupFirestoreOptions(query: Query): FirestoreRecyclerOptions<PickupData> {
return FirestoreRecyclerOptions.Builder<PickupData>()
.setQuery(query, SnapshotParser<PickupData> {
val studentName =
"${it.getString("studentFirstName")} ${it.getString("studentLastName")}"
var atCenter = it.getBoolean("atCenter")!!
val userId = it.id
val pickupTime = it.getString("pickupTime")!!
PickupData(studentName, atCenter, userId, pickupTime)
}).build()
}
private fun absentFirestoreOptions(query: Query): FirestoreRecyclerOptions<AbsentData> {
return FirestoreRecyclerOptions.Builder<AbsentData>()
.setQuery(query, SnapshotParser<AbsentData> {
val studentName =
"${it.getString("studentFirstName")} ${it.getString("studentLastName")}"
AbsentData(studentName)
}).build()
}
缺少适配器代码
class AbsentAdapter(options: FirestoreRecyclerOptions<AbsentData>) :
FirestoreRecyclerAdapter<AbsentData, AbsentAdapter.AbsentAdapterViewHolder>(options) {
class AbsentAdapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbsentAdapterViewHolder {
return AbsentAdapterViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_absentlist, parent, false)
)
}
override fun onBindViewHolder(
holder: AbsentAdapterViewHolder,
position: Int,
model: AbsentData
) {
holder.itemView.apply {
tvStudentOne.text = model.studentName
}
}
}
取件适配器代码
class PickupAdapter(options: FirestoreRecyclerOptions<PickupData>) :
FirestoreRecyclerAdapter<PickupData, PickupAdapter.PickupAdapterViewHolder>(options) {
val db = FirebaseFirestore.getInstance()
class PickupAdapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PickupAdapterViewHolder {
return PickupAdapterViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_pickuplist, parent, false)
)
}
override fun onBindViewHolder(
holder: PickupAdapterViewHolder,
position: Int,
model: PickupData
) {
holder.itemView.apply {
tvStudentName.text = model.studentName
tvPickupTime.text = "Pickup Time: ${model.pickupTime}"
btnSignOut.setOnClickListener {
db.collection("Users").document(model.userId).update("atCenter", !model.atCenter)
val dateTime = LocalDateTime.now()
val date = "${dateTime.monthValue}-${dateTime.dayOfMonth}-${dateTime.year}"
val time = "${dateTime.hour}:${dateTime.minute}"
db.collection("Users").document(model.userId).update("signOutLog", FieldValue.arrayUnion("$date $time"))
db.collection("Users").document(model.userId).update(hashMapOf<String, Any>("pickupTime" to FieldValue.delete()))
}
}
}
}