andi
andi

Reputation: 81

How to refresh the ListView out of the CursorAdapter?

I've got a ListView in my Activity, which is set in the onCreate by

    MyCursorAdapter adapter = new TaskConditsCursorAdapter(this, conditsCursor, taskID, isNewTask);
    setListAdapter(adapter);

Then I do some work in the MyCursorAdapter. Amongst others I have a rowspecific AlertDialog:

...
    builder.setPositiveButton("OK"), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            changeTaskConditBoolValue(taskId, conditId, chosenBoolValue2);
            // refresh?
        }
    });

After the changeTaskConditBoolValue I want to reload my List, since this method changed something in the database, but the List didn't update. requery() results in the list being empty. How can I come "one level above" to work with the adapter again and what should I do to it then?

Thanks a lot!

Upvotes: 2

Views: 8322

Answers (4)

Jackd
Jackd

Reputation: 131

This is what works for me, im not sure its the best way.

c = db.rawQuery( "SELECT * FROM mytable", null); //same line of the initial initialization
adapter.swapCursor(c);

I refresh the only cursor, I dont know what to do with a new one.

Upvotes: 0

calling requery() of adapter is the only solution.

But the method requery() is deprecated. So we can use LoaderManager and Loader.

Upvotes: 1

andi
andi

Reputation: 81

In the MyCursorAdapter in the method called by the onClick - in my case it is changeTaskConditBoolValue(...) I have to call:

this.changeCursor(db.fetchAllTasks(myParameters));

This gets a new cursor by my Database-Class and replaces its own old one with the new one.

Found no other way to change the cursor from inside the CursorAdapter.

Upvotes: 6

azharb
azharb

Reputation: 1009

Just call

adapter.notifyDataSetChanged();

Documentation: Here

Upvotes: 4

Related Questions