kittu
kittu

Reputation: 67

Unable to refresh the list view in realtime after deleting an item from the list

I am having a list view with text and a button. The contents of the listview(the text) are fetched from the SQLite database. I have implemented the code such that, if anyone clicks the button, the corresponding flag of that list item changes in the database table. My intention would be, as soon as the flag is changed in the database table, the corresponding list item should disappear from the list.

Currently, I'm able to change the flag value for the list item (successfully updating in the corresponding column of the table), but the list item remains in the list view until I re-open the app. The list item then disappears as desired.

Is there a way such that as soon as I click the button, and as the flag value is changed at the background, the list refreshes and show me only other items of the list.?

Your help will be much appreciated. Thanks in advance.

Upvotes: 0

Views: 1781

Answers (5)

siefca
siefca

Reputation: 1147

getLoaderManager().restartLoader(0, null, this);

Upvotes: 0

kittu
kittu

Reputation: 67

Thanks all for your responses. I have tried to do it in android way, but seems to be a bit complex. Hence inorder to refresh the list view after updating the list I have re-called the activity using intents so that I'll get a fresh list view with added or deleted list entries.

Hope I'll find other android way of solutions soon.

Upvotes: 0

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

  1. Query with a new Cursor and set the results in the CursorAdapter
  2. invalidate() the existing list view

Upvotes: 0

Huang
Huang

Reputation: 4842

Generally, for a ListView, you have to prepare an adapter for the ListView to provide the content of every item in the ListView. The basic idea is that in order to refresh the listview, you have to call adapter.notifyDataSetChanged() to request the refreshment.

Upvotes: 0

xandy
xandy

Reputation: 27411

You need to do a requery() after changing anything in data source.

-- Edit --

Just found that requery is deprecating, but anyway, the idea is the same. Query for another new Cursor, and set it in CursorAdapter (if you are using it).

Upvotes: 2

Related Questions