Search code examples
androidandroid-listviewandroid-sqliteandroid-cursoradapter

Not getting the Same ListView item before and After insert using Cursor


I have a ListView which by using CursorAdapter I m inflating its rows . And I am using SwapCursor in order to refresh the ListView.

I am also able to get the listView standStill once I add the Content in the DB and Swap the Cursor after that.

Now the Problem is on swapCursor / notifyDatasetChanged / changeCursor it is refreshing the listView.But not maitaining the same item which was present Earlier into the listView.

It is changing the content of item by its previous one once I refresh the listView.

This is How I got ListView standStill after swapCursor() :

if (result_cursor != null) {

            CursorDemo cursorDemo = new CursorDemo(ORMLiteActivity.this , result_cursor);

            cursorDemo.swapCursor(result_cursor);

            View v = listViewDb.getChildAt(0);

            int top = (v == null) ? 0 : v.getTop();

            int firstPosition = listViewDb.getFirstVisiblePosition();

            listViewDb.setAdapter(cursorDemo);

            listViewDb.setSelectionFromTop(firstPosition , top);
            }

So Now I would get the same ListView item before or After swapCursor

Before Insert

After Second

Any Answer is Truely Appreciated...


Solution

  • I figure using this:

        if (result_cursor != null) {
    
                    CursorDemo cursorDemo = new CursorDemo(ORMLiteActivity.this , result_cursor);
    
                    cursorDemo.swapCursor(result_cursor);
    
                    View v = listViewDb.getChildAt(0);
    
                    int top = (v == null) ? 0 : v.getTop();
    
                    int firstPosition = listViewDb.getFirstVisiblePosition();
    
                    int after_insert_position = (firstPosition == 0) ? 0 : firstPosition+1;
    
                    listViewDb.setAdapter(cursorDemo);
    
                    listViewDb.setSelectionFromTop( after_insert_position , top);
    }