现在,我正在研究单击RecyclerView项时显示AlertDialog。 但是,我不知道如何为点击侦听器设置适配器。 你能给我小费吗?
首先,我尝试将AlertDialog放入此主活动中。 这个位置对吗?
MainActivity
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].price}dollar"
}
}
查看持有人
class FoodDataViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),LayoutContainer
答案 0 :(得分:1)
我编辑了您的代码。 MainActivity:
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()
}
}
FoodDataAdapter:
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].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?) {
listener.alertDialog(adapterPosition)
}
}
}
答案 1 :(得分:0)
我只是像这样在适配器的末尾放置了一个'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) {
}
,并且还这样更改了Main活动。
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()
}
}
它有效!但是...一个问题是.. 删除列表后,出现最后一个列表。 列表总数没有减少。.
哈哈 另一个“鸡肉”出现了。.你知道如何解决它吗?