如何将数据从 RecyclerView 传递到 Kotlin 中的 DialogFragment?

Posted

技术标签:

【中文标题】如何将数据从 RecyclerView 传递到 Kotlin 中的 DialogFragment?【英文标题】:How to pass data from RecyclerView to DialogFragment in Kotlin? 【发布时间】:2021-11-14 10:31:19 【问题描述】:

我的 RecyclerView 有一个项目列表,每个项目都有可点击的“删除图标”,它可以从列表中删除项目。如何使用 DialogFragment 确认删除?

这是 FeedAdapter(我的 RecyclerView 适配器)的代码:

class FeedAdapter(private val posts: List<FeedPost>) : RecyclerView.Adapter<FeedAdapter.ViewHolder>()  
        private val listener: (() -> Unit)? = null

fun setListener(listener: (() -> Unit)?) 
        this.listener = listener
    

class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) 
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapt_item, parent, false)
        return ViewHolder(view)
    

override fun onBindViewHolder(holder: ViewHolder, position: Int) 
val post = posts[position]
with(holder.view) 
            user_photo_image.loadUserPhoto(posts[position].photo)
            date_text.text = post.kogda
            caption_text.setCaptionText(nachalo, post.caption)
            test.text = post.ur
            delete_image.setOnClickListener  listener?.invoke()  
           


override fun getItemCount() = posts.size

private fun ImageView.loadUserPhoto(photoUrl: String?) 
            if (!(context as Activity).isDestroyed) 
                GlideApp.with(this).load(photoUrl).fallback(R.drawable.person).into(this)
            
        
    

因此,当您单击 delete_image 时,DeleteDialog 会出现两个按钮 - “确定”和“取消”。如何通过单击“确定”按钮进行确认并从列表中删除项目?

这是我的 DeleteDialog 的代码:

class DeleteDialog : DialogFragment() 
override fun onAttach(context: Context) 
        super.onAttach(context)

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog 
val view = activity!!.layoutInflater.inflate(R.layout.dialog_delete, null)
return AlertDialog.Builder(context).setView(view)
    .setPositiveButton(android.R.string.ok, _, _ -> )
    .setNegativeButton(android.R.string.cancel, _, _ -> )
    .setTitle("Are you sure you want to delete?").create()
    

我试过用这个:

.setPositiveButton(android.R.string.ok, _, _ ->
     mDatabase.child("feed-posts").child("$post.ur").removeValue()

但不幸的是,它无法识别“post.ur”引用,我试图从 ViewHolder 传递它,它代表某些项目的子路径。 我也试图找到另一种在 Net 中实现的变体,但没有成功。

【问题讨论】:

【参考方案1】:

尝试用接口发回对象post,创建接口

interface ClickPost 
fun postClicked(mPost: Post)

在你的适配器上,你在一个变量中实现

class FeedAdapter(private val posts: List<FeedPost>, val click: ClickPost)

点击里面的onBind:

delete_image.setOnClickListener  click.postClicked(post)  

您可以在包含此适配器的片段/活动中将其取回,您可以通过捆绑或参数将帖子发送到 dialogFragment

【讨论】:

感谢您的回答,埃德森。但在这种情况下,我还有另一个问题:如何“通过捆绑或参数将帖子发送到 dialogFragment”?以及如何在“包含适配器”的Activity中初始化变量“post”?再次感谢您的回答! 首先你必须扩展 post 对象:Serializable 以通过包发送,你可以像这样创建包: val myBundle: Bundle() myBunde.putSerializable("keyOrTag", objectPost) 当你调用你这样做的另一个片段 myFragment = myFragment.getInstance() myFragment.bundle = myBundle 然后你调用 supportFragmentManager..... 并发送这个片段。在另一个片段中,您确实喜欢这个 arguments.get("KeyOrTag") 作为 objectPost。像这样您发送并获取对象帖子

以上是关于如何将数据从 RecyclerView 传递到 Kotlin 中的 DialogFragment?的主要内容,如果未能解决你的问题,请参考以下文章