I’m close to finishing my first Android app, but I’m fighting with an odd bug when I delete items from the SQLite DB that feeds a ListView in my app. The item gets deleted, and if I switch away from the ListView and back, it updates, but until I do that, the list doesn’t update.
I’ve posted most of the class in question here: https://gist.github.com/2025973, but here’s the relevant callback starting at line 56 in the file:
builder.setPositiveButton(R.string.button_delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
databaseConnector.deleteSimpleDie(arg3);
storedDiceAdapter.notifyDataSetChanged();
}
});
What am I doing wrong here? I’m sure it’s something simple. I originally had the delete in an Async, but then I couldn’t call the notifyDataSetChanged() at all, because it has to happen from the thread that created the Cursor.
UPDATE
Okay, this is pretty ghetto ( I think… ) but I’ve got it working, finally. Someone want to tell me how horribly wrong this is? Basically, I’m just re-instantiating the whole CursorAdapter inside the callback. It solves the problem, but I suspect it has some negative side effects I’m not aware of.
builder.setPositiveButton(R.string.button_delete,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
databaseConnector.deleteSimpleDie(arg3);
String[] from = new String[] { "name", "description" };
int[] to = new int[] { R.id.dice_name, R.id.dice_description };
storedDiceAdapter = new SimpleCursorAdapter(StoredDice.this,
R.layout.stored_dice_item, null, from, to);
setListAdapter(storedDiceAdapter);
}
});
Well, as per the update on the question, it seems to be working. Not thrilled with that solution, but it’s solved my immediate problem. Thank you everyone for the help.