大家好,又见面了,我是你们的朋友全栈君。
直接调用listview.setOnItemLongClickListener长按事件
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity10.this);
builder.setTitle("提示!");
builder.setMessage("确定删除?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean isSuccess;
// 删除listView选择item的同时删除数据库中对应的信息
Database database = new Database(Activity10.this);
SQLiteDatabase db = database.getWritableDatabase();
String number = list.get(position).get("number");
String name = list.get(position).get("name");
String English_name = list.get(position).get("English_name");
String gender = list.get(position).get("gender");
String age = list.get(position).get("age");
try {
db.execSQL("delete from user where number = '" + number + "' and name = '" + name +
"' and English_name = '" + English_name + "' and gender = '" + gender +
"' and age = '" + age + "'");
Log.e("sql:", "delete from user where number = '" + number + "' and name = '" + name +
"' and English_name = '" + English_name + "' and gender = '" + gender +
"' and age = '" + age + "'");
Log.e("position", String.valueOf(position));
isSuccess = true;
} catch (SQLException e) {
e.printStackTrace();
isSuccess = false;
}
if (list.remove(position) != null) {
System.out.println("Success");
} else {
System.out.println("Failed");
}
if (isSuccess) {
adapter.notifyDataSetChanged();
Toast.makeText(Activity10.this, "已删除", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Activity10.this, "删除失败", Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
// 返回true避免与点击事件冲突
return true;
}
});
效果图:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163285.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...