0

I've been browsing StackOverflow for days now, looking for a solution to my problem, but all the similar questions that have been askes are WAY to complicated for me to understand!

So here it is: I have a ListView, that displays the elements in my SQLite database. For this, it uses a SimpleCursorAdapter. My code is NOT optimized, but I'm just a newbie.

Here's my code:

protected boolean onLongListItemClick(final View v, final int pos, long id) 
        {
           final String str=ReservationList.getItemAtPosition(pos).toString();
           Log.i("ListView", "onLongListItemClick string=" + str);
           AlertDialog.Builder builder = new AlertDialog.Builder(FlightBook.this);
           builder.setMessage("Delete this entry from Flight Book?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
           {
               public void onClick(DialogInterface dialog, int id) 
               {
                   Log.i("ID",""+pos);
                   rdb.removeReservationWithID((int)mAdapter.getItemId(pos));
                   **//NEED SOME CODE HERE TO REFRESH LISTVIEW AUTOMATICALLY**
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() 
           {
               public void onClick(DialogInterface dialog, int id) 
               {
                   dialog.cancel();
               }
           });
           AlertDialog alert = builder.create();
           alert.show();
           return true;
        }

All I need and all I want is to know what should I put in the line I mentioned in the code to make the ListView be refreshed automatically. I tried a notifyDataChanged() on my adapter, but it just won't work (I know I should use threads and very complicated things such as ASyncTask, but too not explained correctly how to use them in this specific case).

Thank you for your aid and please, if you really want to help me, give me CODE, not links! :)

2 Answers 2

0

You need to create a new cursor with the new data and load it into your adapter, like this:

myListAdapter.changeCursor(getCursor());
5
  • And how exactly, in the given code, do I "create a new cursor with the new data"? And where should I do that? Nov 24, 2012 at 16:10
  • You just get a new cursor from your database, exactly like you did in your onCreate or whereever you first set a cursor for your ListView.
    – user1837158
    Nov 24, 2012 at 16:13
  • Done. Not working. If I got you right, here's what I should write: Cursor c = rdb.queueAll(); startManagingCursor(c); mAdapter.changeCursor(c); instead of the line above. But it's not working, my list still dosen't refresh. Nov 24, 2012 at 16:23
  • Is the method you are calling possibly working in a background thread? If so, you have to wait for that to finish.
    – user1837158
    Nov 24, 2012 at 16:34
  • Whoops! Sorry, my bad! I commented the line that deletes the item from the base which is why I thought it wasn't working. :) It does work, actually, you rock, man! Thanks a lot! Nov 24, 2012 at 17:10
0

You should use Loader here.
The LoaderManager is a much more intelligent and generic implementation. startManagingCursor is deprecated as well
An Restart Loader call will refresh your view

Refer
Properly Loading Data
Understanding the LoaderManager

2
  • Whoa! Too complicated, sorry. :( Nov 24, 2012 at 17:02
  • it depends. From my experience I prefer that. My first thought about loader was the same. But now I understood how good it is Nov 24, 2012 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.