【问题标题】:How to use two models for a single recyclerview adapter in kotlin如何在 kotlin 中为单个 recyclerview 适配器使用两个模型
【发布时间】:2022-03-03 17:43:20
【问题描述】:

我有三个模型类,用户名、用户和一个帖子,就像这样

data class User( val uid:String = "" )

data class username( val usernames:String="", )

`data class Post(
        val text:String = "",
        val createdBy: User = User(),
        val createdAt:Long = 0L,
val creates:username = username()

)`

我已将它们连接到 Firebase。 现在我想在一个 recyclerView 适配器中使用这两个模型。我将如何做到这一点? 我的适配器是这样的

    class postadapter(options: FirestoreRecyclerOptions<Post>, val listener: MainActivity) :
    FirestoreRecyclerAdapter<Post, postadapter.postviewholder>(
    options
    ) {
        class postviewholder(itemview: View):RecyclerView.ViewHolder(itemview) {
            val username: TextView = itemview.findViewById(R.id.userrrrrr)
            val createdat: TextView = itemview.findViewById(R.id.createdAt)
            val Text: TextView = itemview.findViewById(R.id.postedText)
    
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): postviewholder {

        val viewholder = postviewholder(LayoutInflater.from(parent.context).inflate(R.layout.feed,parent,false))

        return viewholder
    }
    override fun onBindViewHolder(holder: postviewholder, position: Int ,model: Post) {

        holder.username.text = model.createdBy.uid
        holder.Text.text = model.text
        holder.createdat.text = utils.getTimeAgo(model.createdAt)
    }

}

我还想将用户名模型的值传递给“creates”的帖子模型 另外,在 onbindviewholder 中,我想传递我在数据类中创建的用户名的值。

【问题讨论】:

标签: android firebase android-studio kotlin android-recyclerview


【解决方案1】:

不需要两个模型,只要把外面的所有东西都放在里面。然后使用一个标识符通过模型名来访问它。

DATABASE {
   MODEL_ID
        {
          MODEL_ID : 1000
          1000 {
                  EXAMPLE_DATA_ONE: 1001
                  EXAMPLE_DATA_TWO: 1002

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-30
    • 2019-10-28
    • 2017-04-02
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2022-01-05
    • 2018-11-22
    相关资源
    最近更新 更多