Reputation: 683
So I have a small problem, my code deletes ListView row from ListView but every time I kill the app and then reopen it the "deleted" rows populate the ListView again.
Here's the code for delete method in DatabaseHelper class:
public void obrisiTrening(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DBKonstante.TABLE_NAME, DBKonstante.KEY_ID + "=?", new String[]{String.valueOf(id)});
db.close();
And here's what my code for deleting ListView row and record from database:
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;
}
}
Upvotes: 0
Views: 61
Reputation: 191738
Not sure what this is trying to do.
dba.obrisiTrening(idToDelete);
dba = new DBHandler(MainActivity.this);
dba.obrisiTrening(i);
You only need this
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();
And note: you shouldnt be using an Arraylist & ArrayAdapter here... You are using a database, so CursorAdapter is what you want
Upvotes: 1