CARREAU Clément
CARREAU Clément

Reputation: 717

notifyDataSetChanged with SQLite

I'm sorry to ask about what seems to be a simple problem but I've been looking for a solution for hours and now I can't stand it anymore.

My app is very simple. I've got "tasks" in a SQLite db, and I populate a listView with this db like this :

_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);

which returns an arrayList of Task.

Problem is, when I click the dialog to remove a task, it removes it from the db but it's still on the screen, that's why I need to refresh the listView.

 case DialogInterface.BUTTON_NEUTRAL:
                _taskDAO.removeTask(1);
                _dataAdapter.notifyDataSetChanged();
                break;

But it does not work. I think it's due to the fact that I remove the task from the db but not from the adapter but I'm not quite sure. Anyway, if someone's got a solution ...

Upvotes: 0

Views: 1641

Answers (2)

An-droid
An-droid

Reputation: 6485

If your adapter is an ArrayAdapter or BaseAdapter, the data source of your ListView is not the DB itself but the List you provide to the adapter.

You must update the datasource of your adapter (give him a new list) then call

_dataAdapter.notifyDataSetChanged();

Another solution would be to recreate the adapter :

_dataAdapter = null;
_dataAdapter = new TaskAdapter(_c, _taskDAO.getTasks());
_listTask.setAdapter(_dataAdapter);

Hope it helps

Upvotes: 1

Prachi
Prachi

Reputation: 3672

try,

(listName.getAdapter()).notifyDataSetChanged();

Upvotes: 0

Related Questions