gpt4 book ai didi

android - RecyclerView 不显示来自 firebase 的数据(Kotlin)

转载 作者:行者123 更新时间:2023-11-30 05:09:51 27 4
gpt4 key购买 nike

谁能帮帮我?我尝试在线学习 android-firebase,但它是旧的。所以代码已经改变了。我被困在“onBindViewHolder”和“onCreateViewHolder”因为类(class)没有那个。

class UsersAdapter(databaseQuery: DatabaseReference, var context: Context):
FirebaseRecyclerAdapter<Users, ViewHolder>(
FirebaseRecyclerOptions.Builder<Users>().setQuery(databaseQuery, Users::class.java).build()
) {
override fun onBindViewHolder(holder: ViewHolder, position: Int, model: Users) {

var userID = getRef(position).key

holder.bindView(model, context)

holder.itemView.setOnClickListener {
//todo create a popup dialog where user can do
Toast.makeText(context, "user row click $userID", Toast.LENGTH_LONG).show()
}


}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

var view = LayoutInflater.from(p0.getContext())
.inflate(R.layout.friendlist_row, p0, false)

return ViewHolder(view,context)


}

}

上面是我的 RecycleView 适配器

class ViewHolder(itemView: View, context: Context): RecyclerView.ViewHolder(itemView) {

var userNameTxt: String ? = null
var userStatusTxt: String ? = null
var userProfilePicLink: String ? = null

fun bindView(user: Users, context: Context) {
var userName = itemView.findViewById < TextView > (R.id.friendlistNameTXID)
var userStatus = itemView.findViewById < TextView > (R.id.friendlistStatusTXID)
// var userProfilePic = itemView.findViewById<CircleImageView>(R.id.friendlistImageIVID)

//set the string so we can pass in the intent
userNameTxt = user.usernickname
userStatusTxt = user.userstatus
// userProfilePicLink = user.thumb_image

userName.text = userNameTxt
userStatus.text = userStatusTxt

// Picasso.get()
// .load(userProfilePicLink)
// .placeholder(R.drawable.ic_launcher_foreground)
// .into(userProfilePic)
}
}

上面是我的 Adapter 的 ViewHolder

class UsersFragment: Fragment() {
var mUserDatabase: DatabaseReference ? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup ? ,
savedInstanceState : Bundle ?
): View ? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_user, container, false)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle ? ) {
super.onViewCreated(view, savedInstanceState)

var linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
mUserDatabase = FirebaseDatabase.getInstance().reference.child("Users")

friendlistRVID.setHasFixedSize(true)
friendlistRVID.layoutManager = linearLayoutManager
friendlistRVID.adapter = UsersAdapter(mUserDatabase!!, context!!)

}

}

上面是我使用的 fragment ,里面有 RecycleView

它不会显示任何内容,也没有错误。提前谢谢你

最佳答案

您是否在适配器中使用了 itemCount

override fun getItemCount(): Int {
return count
}

关于android - RecyclerView 不显示来自 firebase 的数据(Kotlin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com