简体   繁体   English

Firebase RecyclerView Adapter onBindViewHolder

[英]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.

相关问题 RecyclerView适配器:OnBindViewHolder - RecyclerView Adapter :OnBindViewHolder Recyclerview 适配器 onBindViewHolder 有效负载不起作用 - Recyclerview adapter onBindViewHolder payload is not working 在 RecyclerView 适配器的 onBindViewHolder 中获取上下文 - Get context in onBindViewHolder in RecyclerView Adapter RecyclerVIew适配器onBIndVIewHolder中的上下文错误 - context error in RecyclerVIew Adapter onBIndVIewHolder 在 onBindViewHolder RecyclerView Firebase 适配器中检索用户 uid 详细信息 | FirebaseUI | Android,Java - Retrive user uid details in onBindViewHolder RecyclerView Firebase Adapter | FirebaseUI | Android ,Java 使用RecyclerView.Adapter在onBindViewHolder()内设置onItemClickListener - Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter RecyclerView.Adapter onBindViewHolder()获取错误的位置 - RecyclerView.Adapter onBindViewHolder() gets wrong position Recyclerview 不调用任何 Adapter 方法:onCreateViewHolder、onBindViewHolder - Recyclerview not call any Adapter method: onCreateViewHolder, onBindViewHolder 如何在RecyclerView适配器的onBindViewHolder中声明overridePendingTransition? - How to declare overridePendingTransition inside onBindViewHolder of a RecyclerView adapter? RecyclerView Adapter notifyDataSetChanged()之后未调用onBindViewHolder - onBindViewHolder not called after RecyclerView Adapter notifyDataSetChanged()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM