Android ListView update with SimpleCursorAdapter
Asked Answered
W

3

5

Hey i use a listview for demonstrate entries which are stored in a database. I also have a EditText element and a button which adds the content of the EditText into the Database. To bind the view to the database content i use the SimpleCursorAdapter and following populate function:

private void populate() {
    cursor = dbAdapter.getAllItems();
    startManagingCursor(cursor);

    String[] from = new String[] { DBAdapter.KEY_TASK };
    int[] to = new int[] { android.R.id.text1 };

    // Now create an array adapter and set it to display using our row
    cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
    list.setAdapter(cursorAdapter);
}

If i added a new entry, by clicking on the button i want to refresh the listview but this only works with the populate function and not with the common adapter function notifyDataSetChanged();. Do i have a bug or is this the right way to refresh a listview?

Wake answered 21/11, 2011 at 14:17 Comment(0)
N
9

Have you seen this, tried the swap cursor method, or tried just simply calling setAdapter() again?

I had a similar issue where I could not get my list to update, and what I did was just create a refreshListView() method. Now you can call this initially from your onCreate(), AND anytime a user adds something to the DB. All it does is re-bind the listview to a cursor. With all the deprecating methods (requery()), and issues with notifyDataSetChanged(), I decided this was the easiest way.

Nystatin answered 21/11, 2011 at 14:28 Comment(5)
Just to mention: 'requery()' is already deprecated. Documentation has a clear record on this.Outclass
I know, I didn't quite word that sentence right, re-worded it :>Nystatin
Hey Jack thanks for your reply, i see that i forget to say the above code is my populate() function i call it in my onCreate() method and if i click the "Add"-Button. I only wanted to know whether this is a good solution to call this method instead of calling the common notifyDataSetChanged() method of the base adapter class.Wake
Oh I see, it is too early and I have not had my coffee yet. Yes that should be fine, I've been using it for awhile now and it works great.Nystatin
Hehe...ok no problem sometimes i have the same problem without coffee or red bull and in fact it was my false. But thanks for your help.Wake
Q
3

Please refer this link...it works like charm

Update SimpleCursorAdapter while maintaining scroll position in ListView

for dynamic listview on scroll i added new item from database .. I did mistake here .. i was assigning new adapter for each time for same simplecursoradapter . Instead of creating new adapter. just use

adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
 lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);
Quadric answered 8/3, 2013 at 9:31 Comment(1)
found very good hackYellowthroat
M
1

You need to call swapcursor() before notifyDataSetChanged() on the adapter.

Mcloughlin answered 18/9, 2014 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.