Kotlin:单击 RecycleView 项目时显示 AlertDialog

Posted

技术标签:

【中文标题】Kotlin:单击 RecycleView 项目时显示 AlertDialog【英文标题】:Kotlin: Show AlertDialog when RecycleView item clicked 【发布时间】:2020-07-03 14:45:59 【问题描述】:

现在我正在研究在单击 RecyclerView 项目时显示 AlertDialog。 但是,我不知道如何为点击监听器设置适配器。 你能给我一个小费吗?

首先,我尝试将 AlertDialog 放在这个 Main Activity 中。 这是正确的位置吗?

主活动

class MainActivity : AppCompatActivity() 

    private val foodList = listOf(
        FoodModel("Noodle", 2),
        FoodModel("Cake", 3),
        FoodModel("Pizza", 4),
        FoodModel("Stake", 5),
        FoodModel("Chicken", 4)
    )

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val adapter = FoodDataAdapter(foodList)
        adapter.notifyDataSetChanged()
        foodListView.adapter = adapter
        foodListView.layoutManager = LinearLayoutManager(this)

        fun onItemClick(item: FoodModel, position: Int) 
            val dialog = AlertDialog.Builder(this)
            dialog.setTitle("Item deletion")
            dialog.setMessage("Do you want to delete this item?")
            dialog.setPositiveButton("Yes", DialogInterface.OnClickListener  _, _ ->
            )
            dialog.setNegativeButton("No", DialogInterface.OnClickListener  _, _ ->
            )
            dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener  _, _ ->
            )
            dialog.show()
        
    

这是适配器。

class FoodDataAdapter(val list: List<FoodModel>):RecyclerView.Adapter<FoodDataViewHolder>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food,parent,false)
        return FoodDataViewHolder(view)
    

    override fun getItemCount(): Int 
       return list.count()
    

    override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) 
        holder.containerView.nameText.text=list[position].name
        holder.containerView.priceText.text="$list[position].pricedollar"
    

视图支架

class FoodDataViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),LayoutContainer

【问题讨论】:

【参考方案1】:

我编辑了你的代码。主要活动:

class MainActivity : AppCompatActivity(), FoodDataAdapter.OnItemClickListener 

var foodList = ArrayList<FoodModel>()
lateinit var adapter : FoodDataAdapter
override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    foodlist.add(FoodModel("Noodle", 2))
    foodlist.add(FoodModel("Cake", 3))
    foodlist.add(FoodModel("Pizza", 4))
    foodlist.add(FoodModel("Stake", 5))
    foodlist.add(FoodModel("Chicken", 4))

    adapter = FoodDataAdapter(foodList, this)
    foodListView.adapter = adapter
    foodListView.layoutManager = LinearLayoutManager(this)



override fun alertDialog(position: Int) 
    val dialog = AlertDialog.Builder(this)
    dialog.setTitle("Item deletion")
    dialog.setMessage("Do you want to delete this item?")
    dialog.setPositiveButton("Yes", DialogInterface.OnClickListener  _, _ -> 
    adapter.removeItem(position)
    )
    dialog.setNegativeButton("No", DialogInterface.OnClickListener  _, _ ->
    )
    dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener  _, _ ->
    )
    dialog.show()


食物数据适配器:

class FoodDataAdapter(var list: ArrayList<FoodModel>, var listener : FoodDataAdapter.OnItemClickListener):RecyclerView.Adapter<FoodDataAdapter.FoodDataViewHolder>()

interface OnItemClickListener 
    fun alertDialog(position : Int)


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder 
    val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food,parent,false)
    return FoodDataViewHolder(view)


override fun getItemCount(): Int 
    return list.count()


fun removeItem(position: Int) 
    list.removeAt(position)
    notifyItemRemoved(position)


override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) 
    holder.nameText.text=list[position].name
    holder.priceText.text="$list[position].pricedollar"


inner class FoodDataViewHolder(containerView: View) : RecyclerView.ViewHolder(containerView),
    View.OnClickListener 

    var nameText: TextView = containerView.findViewById(R.id.nameText)
    var priceText: TextView = containerView.findViewById(R.id.priceText)

    init 
        containerView.setOnClickListener(this)
    

    override fun onClick(v: View?) 
        listener.alertDialog(adapterPosition)
    


【讨论】:

不过,我可以再问一件事吗?当我单击 AlertDialog 中的“是”按钮时,如何删除选定的列表? 嗯..我有两个错误。一个来自 FoodDataAdapter 中的list.remove(position)。消息是“未解析的引用。由于接收器类型不匹配,以下候选者都不适用。第二个错误是在主要活动中,即var adapter: FoodDataAdapter = null。消息是“Null 不能是非空类型的值FoodDataAdapter" list.removeAt(position),lateinit var 适配器:FoodDataAdapter 它仍然是错误,“未解决的参考”。我认为我们需要声明“removeAt”。你觉得怎么样? 我想,你使用了arraylist。将您的列表更改为 ArrayList。然后就可以了【参考方案2】:

我只是像这样在适配器的末尾添加了“removeAt”的声明代码,

class FoodDataAdapter(val list: List<FoodModel>,var itemClicklistener : OnItemClickListener):RecyclerView.Adapter<FoodDataAdapter.FoodDataViewHolder>() 

    interface OnItemClickListener 
        fun onItemClick(position: Int)
    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder 
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food, parent, false)
        return FoodDataViewHolder(view)
    

    override fun getItemCount(): Int 
        return list.count()
    

    fun removeItem(position: Int) 
        list.removeAt(position)
        notifyItemRemoved(position)
    

    override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) 
        holder.nameText.text = list[position].name
        holder.priceText.text = "$list[position].price dollar"
    

    inner class FoodDataViewHolder(containerView: View) : RecyclerView.ViewHolder(containerView),
        View.OnClickListener 
        var nameText: TextView = containerView.findViewById(R.id.nameText)
        var priceText: TextView = containerView.findViewById(R.id.priceText)

        init 
            containerView.setOnClickListener(this)
        

        override fun onClick(v: View?) 
            itemClicklistener.onItemClick(adapterPosition)
        
    


private fun Any.removeAt(position: Int) 


并且还像这样更改了主要活动。

class MainActivity : AppCompatActivity(), FoodDataAdapter.OnItemClickListener 

    private val foodList = arrayListOf(
        FoodModel("Noodle", 3),
        FoodModel("Cake", 5),
        FoodModel("Pizza", 7),
        FoodModel("Stake", 8),
        FoodModel("Chicken", 8)
    )
    var adapter: FoodDataAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        adapter = FoodDataAdapter(foodList, this)
        foodListView.adapter = adapter
        foodListView.layoutManager = LinearLayoutManager(this)
    

    override fun onItemClick(position: Int) 
        val dialog = AlertDialog.Builder(this)
        dialog.setTitle("Item deletion")
        dialog.setMessage("Do you want to delete this item?")
        dialog.setPositiveButton("Yes", DialogInterface.OnClickListener  _, _ ->
            adapter?.removeItem(position)
        )
        dialog.setNegativeButton("No", DialogInterface.OnClickListener  _, _ ->
        )
        dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener  _, _ ->
        )
        dialog.show()
    

而且它有效!但是……一个问题是…… 删除列表后,最后一个列表保留出现。 列表总数没有减少..

哈哈 又一个“鸡”出现了。你知道怎么解决吗?

【讨论】:

像我的回答一样将您的列表转换为 arrayList。并删除行 Any.removeAt(position: Int)

以上是关于Kotlin:单击 RecycleView 项目时显示 AlertDialog的主要内容,如果未能解决你的问题,请参考以下文章

想要删除recycleview上的一个项目。单击按钮删除单击。该项目在firebase数据库中删除但在循环视图中该项目存在

在 RecyclerView 中单击时使用 Picasso 或任何图像加载器重新加载图像

[Android Studio]如何从Fragment中嵌套的Recycleview启动一个Activity?

Kotlin:获取点击项目列表视图的值(片段+适配器)

spinner android kotlin 的自定义适配器不可见,但在单击下拉值时可见

RecycleView 不从 Google Firestore 检索数据