所以我遇到一个小问题,我的代码会从ListView中删除ListView行,但每次我杀死应用程序然后重新打开它时,“已删除”行会再次填充ListView。
这是DatabaseHelper类中delete方法的代码:
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;
}
}
答案 0 :(得分:1)
不确定这是做什么的。
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&amp; ArrayAdapter here ...您正在使用数据库,因此CursorAdapter就是您想要的