我有这个ListView
包含的项目,我想创建一个警告对话框,当我长按项目时删除任何这些项目。项目上的onLongClick
显示AlertDialog
,如果我点击是,则会移除该项目。
这是我的代码。
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
notesList.remove(i);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});
答案 0 :(得分:3)
我认为问题是'i'警告对话框点击列表器的位置,你需要用户列出项目点击位置才能从列表中删除项目。
请使用以下代码:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
if(notesList!=null){
notesList.remove(position);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});
答案 1 :(得分:0)
const char *sql = "PRAGMA table_info(family)"; /* 'family' is the table name */
这对我有用...试试这个