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?

Siggy Petersen
  • 195
  • 3
  • 6
  • 16

3 Answers3

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.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • Just to mention: 'requery()' is already deprecated. [Documentation](http://developer.android.com/reference/android/database/Cursor.html#requery()) has a clear record on this. – 87element Nov 21 '11 at 14:31
  • I know, I didn't quite word that sentence right, re-worded it :> – Jack Nov 21 '11 at 14:34
  • 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. – Siggy Petersen Nov 21 '11 at 14:37
  • 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. – Jack Nov 21 '11 at 14:40
  • 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. – Siggy Petersen Nov 21 '11 at 14:41
2

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);
Community
  • 1
  • 1
Pranita Patil
  • 791
  • 1
  • 9
  • 16
1

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

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
maya
  • 11
  • 1