[英]Firebase RecyclerView Adapter onBindViewHolder
I'm trying to get data from Firebase to my RecyclerView
by FirebaseRecyclerAdapter
but it always set the list item to the initial value of the class我正在尝试通过FirebaseRecyclerAdapter
从 Firebase 到我的RecyclerView
获取数据,但它总是将列表项设置为 class 的初始值
I am using data binding to set the class to a variable person.我正在使用数据绑定将 class 设置为变量 person。
Adapter class:适配器 class:
class RecyclerAdapterAllUser(options: FirebaseRecyclerOptions<PersonalInfo>) :
FirebaseRecyclerAdapter<PersonalInfo, RecyclerAdapterAllUser.Holder>(options) {
private lateinit var binding: ListItemAllUserRecyclerBinding
class Holder(val binding: ListItemAllUserRecyclerBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onBindViewHolder(holder: Holder, position: Int, model: PersonalInfo) {
binding.person = model
}
}
class class
class PersonalInfo() {
var name: String =""
var nickName: String = ""
private var age = 0
constructor(name: String, nickName: String, age: Int) : this() {
this.name = name
this.age = age
this.nickName = nickName
}
}
activity活动
class AllUsers : AppCompatActivity() {
private lateinit var binding: ActivityAllUsresBinding
private lateinit var adapter: RecyclerAdapterAllUser
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_all_usres)
binding.listItemAllUsersActivity.hasFixedSize()
binding.listItemAllUsersActivity.layoutManager = LinearLayoutManager(this)
val options = FirebaseRecyclerOptions.Builder<PersonalInfo>()
.setQuery(
FirebaseDatabase.getInstance().getReference("users"),
PersonalInfo::class.java
)
.build()
adapter = RecyclerAdapterAllUser(options)
binding.listItemAllUsersActivity.adapter = adapter
}
override fun onStart() {
super.onStart()
adapter.startListening()
}
override fun onStop() {
super.onStop()
adapter.stopListening()
}
}
firebase database firebase数据库
chat-app-3ce78
users
8tS5NqyvQUaLpo7oVbhCbhDnud82
LogIn
PersonalInfo
age:18
name: "mohamed"
nickName: "komy"
fTppKQYGMxbBPkMoM7A2aMwch332
The problem in your code lies in the fact that you're trying to use the following reference:您的代码中的问题在于您尝试使用以下参考:
FirebaseDatabase.getInstance().getReference("users")
To display in you RecyclerView PersonalInfo
objects.在您的 RecyclerView PersonalInfo
对象中显示。 Please note, that this not possible since under users
node, there are actually no such objects.请注意,这是不可能的,因为在users
节点下,实际上没有这样的对象。 An object of type PersonalInfo
exists under PersonalInfo
node, hence that behavior. PersonalInfo
节点下存在类型为PersonalInfo
的 object,因此存在这种行为。 Besides that, you also missing a child, which is 8tS5NqyvQUaLpo7oVbhCbhDnud82
, being the UID of the user that comes from the authentication process.除此之外,您还缺少一个孩子,即8tS5NqyvQUaLpo7oVbhCbhDnud82
,它是来自身份验证过程的用户的 UID。
To solve this, you should use a query that looks like this:要解决这个问题,您应该使用如下所示的查询:
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().getReference()
val usersRef = rootRef.child("users")
val query = usersRef.orderByChild(uid + "/PersonalInfo/name")
This query
object should be passed to the setQuery()
method in order to get those objects displayed.此query
object 应传递给setQuery()
方法,以便显示这些对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.