2

My cursor is fetch data from sqlite.

msgsdbadapter = new MSGSDBAdapter(this);
msgsdbadapter.open();
cons_cursor = msgsdbadapter.fetchConversations();
startManagingCursor(cons_cursor);

After that I create a SimpleCursorAdapter and assign it to ListView. The ListView now can display some records well.

String[] from = new String[] { MSGSDBAdapter.KEY_FROM, MSGSDBAdapter.KEY_MSG, MSGSDBAdapter.KEY_DATE};
int[] to = new int[] { R.id.lblFrom, R.id.lblMsgExcerpt, R.id.lblDate };
cons_cursor_adapter = new SimpleCursorAdapter(context, R.layout.conversation_item, cons_cursor, from, to);
lvConversations.setAdapter(cons_cursor_adapter);

Next, I insert new row into table and notify dataset changed, but the ListView is not update

msgsdbadapter.createMsg(msg);
cons_cursor_adapter.notifyDataSetChanged();

And when I should close the db connection?

3
  • 2
    No need to close it before onDestroy Commented Oct 25, 2011 at 8:38
  • I think u need to update cursor that u have passed to adapter. Commented Oct 25, 2011 at 8:46
  • How to update cursor? I can create new cursor and assign it again to adapter. But that way seem bad? Commented Oct 25, 2011 at 9:24

2 Answers 2

7

You need to either requery() the existing Cursor, or run the query again to get a fresh Cursor and swap the new Cursor into your CursorAdapter.

Sign up to request clarification or add additional context in comments.

3 Comments

requery is decaperpted. Are there have a new approach for this problem?
@complez: Run the query again to get a fresh Cursor and swap the new Cursor into your CursorAdapter using changeCursor().
This updated solution works just fine, but I'm wondering why the adapter.notifyDataSetChanged() is not able to do this itself?
0

The best solution if your using the ListFragment is to put this in inside this sample:

 public SimpleCursorAdapter myNote;
    public class NOteLIST_MAIN extends ListFragment {

    @SuppressWarnings("deprecation")
    @Override
    public void onResume() {
        super.onResume();
        myNote.getCursor().requery();//Don't forget to put these on onResume }

    private void DataLoader(){private void DataFiller(){
          myNote = new SimpleCursorAdapter(getActivity(), R.layout.notes_row_line, notesCursor, from, to,0);
          setListAdapter(myNote);}  
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.