将项目添加到现有的 AlertDialog
Adding items to an existing AlertDialog
所以我需要显示一个带有一个项目的AlertDialog,然后执行一个get请求,在它解决之后,我需要向对话框添加更多项目。
我尝试了下面的代码但没有成功
var list = arrayOf("aa")
val b = AlertDialog.Builder(context)
b.setItems(list, onItemClickListener)
val dialog = b.create()
executeGetRequest() {
list += "New Item"
val adapter = dialog.listView.adapter as ArrayAdapter<*>
adapter.notifyDataSetChanged()
}
但是,正在更新现有项目:
list[0] = "Updated Item"
adapter.notifyDataSetChanged()
我错过了什么?
已解决
val list = arrayListOf("aa")
val b = AlertDialog.Builder(context)
val onItemClickListener = DialogInterface.OnClickListener { dialog, which ->
// code
}
val adapter = ArrayAdapter(requireContext(),
android.R.layout.simple_list_item_1, list)
setAdapter(adapter, onItemClickListener)
executeGetRequest() {
list.add("New Item")
adapter.notifyDataSetChanged()
}
b.show()
所以我需要显示一个带有一个项目的AlertDialog,然后执行一个get请求,在它解决之后,我需要向对话框添加更多项目。
我尝试了下面的代码但没有成功
var list = arrayOf("aa")
val b = AlertDialog.Builder(context)
b.setItems(list, onItemClickListener)
val dialog = b.create()
executeGetRequest() {
list += "New Item"
val adapter = dialog.listView.adapter as ArrayAdapter<*>
adapter.notifyDataSetChanged()
}
但是,正在更新现有项目:
list[0] = "Updated Item"
adapter.notifyDataSetChanged()
我错过了什么?
已解决
val list = arrayListOf("aa")
val b = AlertDialog.Builder(context)
val onItemClickListener = DialogInterface.OnClickListener { dialog, which ->
// code
}
val adapter = ArrayAdapter(requireContext(),
android.R.layout.simple_list_item_1, list)
setAdapter(adapter, onItemClickListener)
executeGetRequest() {
list.add("New Item")
adapter.notifyDataSetChanged()
}
b.show()