gpt4 book ai didi

java - Android studio 使用Split方法能够从数据库中删除待办事项列表

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:21 25 4
gpt4 key购买 nike

我使用 onItemLongClickListener 方法在长按列表项时创建删除事件。我在 DAO 类中有删除任务方法。如何在 LongClick 方法中实现它?

TaskList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, final long l) {

//Get the selected item position
selectedItem = adapterView.getItemAtPosition(position).toString();

//Create a dialog box to inform the delete
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
//Display the seletced item name
builder.setMessage("Do you want to remove " + selectedItem + "?");

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

String[] list = selectedItem.split(":");
//Missing something here that I'm not sure

if (dao.deleteTask(id)) {
Toast.makeText(MainActivity.this, "Deleted" + task[position], Toast.LENGTH_SHORT).show();
}
}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});

builder.show();

return true;
}
});
<小时/>
public boolean deleteTask(int id) {
//Get a reference to the database
SQLiteDatabase db = this.getReadableDatabase();
//Excute a sql query and get a results in Cursor object "res"
db.execSQL("Delete from tasks where id = " + id);

return true;
}
<小时/>

最佳答案

将您的 ID 存储在单独的列表中,例如

int idList[] = {1, 2, 3, 4, 5};
TaskList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, final long l) {

//Create a dialog box to inform the delete
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
//Display the seletced item name
builder.setMessage("Do you want to remove " + selectedItem + "?");

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {


if (dao.deleteTask(idList[position])) {

}
}
});


builder.show();

return true;
}
});

关于java - Android studio 使用Split方法能够从数据库中删除待办事项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48955397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com