【发布时间】:2014-04-12 19:18:20
【问题描述】:
我有一个 ListView 显示不同配置文件的名称,当用户点击一个项目时,活动显示一个 alertDialog,如果用户按下确认按钮,我想从我的 listView 中删除该元素,从我的 ArrayAdapter 和来自我的 ArrayList。 我知道 onItemLongClick 方法中的 arg2 表示所选项目的索引,但我希望能够在肯定按钮的 onClick 方法中访问它。 有什么建议吗? 我的 ArrayList 称为“ListaUtentiStringa”,ArrayAdapter 是“profilesAdapter”,listView 称为 listview。 对不起,我的英语不好。
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Vuoi davvero cancellare il profilo?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
}
});
builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
profilesAdapter.notifyDataSetChanged();
return true;
}
});
【问题讨论】:
-
您可以将项目的位置传递给警报对话框,如果用户按下删除按钮,那么您只需删除该位置的项目!
-
如何通过该职位?我无法通过 arg2
标签: android listview android-listview onclick