- 编辑**
我改变了我的代码并且得到了一个结果但是它把数据分解成分开的值.它从数据库中为每个孩子读取所有数据.我有2个孩子但是它只检索最后一个孩子的数据并且分开显示它的值.它从变量eta_text
存储它的值
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
clear_all = view.findViewById(R.id.clear_all)
notificationView = view.findViewById(R.id.notificationList)
notificationArray = arrayListOf()
getNotifData()
var linearLayoutManager = LinearLayoutManager(context)
notificationView.layoutManager = linearLayoutManager
notificationView.setHasFixedSize(true)
}
private fun getNotifData() {
val user = FirebaseAuth.getInstance().currentUser
val useremail = user!!.email
dbref = FirebaseDatabase.getInstance().reference
dbref.child("Students").orderByChild("email").equalTo(useremail.toString()).addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
for (ds in snapshot.children) {
val idNumber: String? = ds.key
dbref.child("Notification").child(idNumber.toString()).addValueEventListener(object : ValueEventListener{
override fun onDataChange(dsnapshot: DataSnapshot) {
for (dsd in dsnapshot.children) {
val key: String? = dsd.key
dbref.child("Notification").child(idNumber.toString()).child(key.toString()).addValueEventListener(object : ValueEventListener{
override fun onDataChange(dsnap: DataSnapshot) {
notificationArray.clear()
if (dsnap.exists()){
for (queueSnapshot in dsnap.children){
notificationArray.add(Notification(queueSnapshot.value.toString()))
}
notifadapter = MyAdapter_Notification(notificationArray)
notificationView.adapter = notifadapter
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
我的数据类
package com.example.sqms
data class Notification(var eta_text : String ?= null,
var office_name : String ?= null,
var time_text : String ?= null)
我的适配器
class MyAdapter_Notification (private val notificationList : ArrayList<Notification>)
: RecyclerView.Adapter<MyAdapter_Notification.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val notificationView = LayoutInflater.from(parent.context).inflate(R.layout.notification_view,parent,false)
return MyViewHolder(notificationView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = notificationList[position]
holder.eta_text.text = currentItem.eta_text
holder.office_name.text = currentItem.office_name
holder.time_text.text = currentItem.time_text
}
override fun getItemCount(): Int {
return notificationList.size
}
inner class MyViewHolder(notificationView : View) : RecyclerView.ViewHolder(notificationView){
val eta_text : TextView = itemView.findViewById(R.id.eta_text)
val office_name : TextView = itemView.findViewById(R.id.office_name)
val time_text : TextView = itemView.findViewById(R.id.time_text)
}
}
下图是来自数据库database的值
下图是单独显示的数据notification recycler view
1条答案
按热度按时间vfh0ocws1#
我只是解决了它。我只是删除了第三个嵌套的数据库引用并更改了数据写入