gpt4 book ai didi

java - Android - 从 ListView 中删除项目,但不从数据库中删除项目

转载 作者:行者123 更新时间:2023-12-02 13:32:15 25 4
gpt4 key购买 nike

所以我有一个小问题,我的代码从 ListView 中删除 ListView 行,但每次我终止应用程序然后重新打开它时,“已删除”的行都会再次填充 ListView。

这是DatabaseHelper类中删除方法的代码:

 public void obrisiTrening(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DBKonstante.TABLE_NAME, DBKonstante.KEY_ID + "=?", new String[]{String.valueOf(id)});
db.close();

这是我从数据库中删除 ListView 行和记录的代码:

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


final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_delete);

final TextView tvDialogDelete = (TextView) dialog.findViewById(R.id.tvDialogDelete);

tvDialogDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DBPodatci infoData = dbPodatci.get(i);
dba = new DBHandler(MainActivity.this);
int position = dbPodatci.indexOf(infoData);
dbPodatci.remove(position);

DBPodatci podatki = new DBPodatci();
final int idToDelete = podatki.getItemId();

dba.obrisiTrening(idToDelete);
dba = new DBHandler(MainActivity.this);
dba.obrisiTrening(i);

rec_WorkoutItemsList.setAdapter(vjezbaAdapter);
vjezbaAdapter.notifyDataSetChanged();
dialog.dismiss();

}
});
dialog.show();

return false;
}
});

DB PODATCI

public class DBPodatci {
public String odabraneVjezbe, recordDate;
public int itemId;

public String getOdabraneVjezbe() {
return odabraneVjezbe;
}

public void setOdabraneVjezbe(String odabraneVjezbe) {
this.odabraneVjezbe = odabraneVjezbe;
}

public String getRecordDate() {
return recordDate;
}

public void setRecordDate(String recordDate) {

this.recordDate = recordDate;
}

public int getItemId() {
return itemId;
}

public void setItemId(int itemId) {
this.itemId = itemId;
}

}

最佳答案

不确定这要做什么。

dba.obrisiTrening(idToDelete);
dba = new DBHandler(MainActivity.this);
dba.obrisiTrening(i);

你只需要这个

   final TextView tvDialogDelete = (TextView) dialog.findViewById(R.id.tvDialogDelete);

final DBHandler dba = new DBHandler(MainActivity.this);

tvDialogDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DBPodatci infoData = dbPodatci.get(i);
final int idToDelete = infoData.getItemId();

dbPodatci.remove(i);
dba.obrisiTrening(idToDelete);

vjezbaAdapter.notifyDataSetChanged();
dialog.dismiss();

注意:你不应该在这里使用 Arraylist 和 ArrayAdapter...你正在使用数据库,所以 CursorAdapter 就是你想要的

关于java - Android - 从 ListView 中删除项目,但不从数据库中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43140425/

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