Question

I try to do an application of Notes but when I add a note with an editor i made but the ListActivity is not update. I try to refresh my activityList after add something in the database but it's not working. Here my code :

public class NoteList extends ListActivity {
SimpleCursorAdapter adapter;
DatabaseSQLiteHelper db;


/**
 * Update the list.
 */
private void updateView() {
    adapter.notifyDataSetChanged();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new NotappSQLiteHelper(this);
    // Run this function the first time for adding something to db 
    //  db.testClass();
    Cursor cursor = db.getNotes();
    adapter = new SimpleCursorAdapter(this,
            R.layout.notelist_item, 
            cursor,
            new String[]{NotappSQLiteHelper.COLUMN_TITLE,
            NotappSQLiteHelper.COLUMN_DATE,
            NotappSQLiteHelper.COLUMN_TEXT},
            new int[] {R.id.title_item_list,
            R.id.date_item_list,
            R.id.text_item_list},

            android.support.v4.widget.
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    setListAdapter(adapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.addNote: 
        Intent intent = new Intent(this, NoteEditor.class);
        startActivityForResult(intent, 0);
        updateView();
        break;

    case R.id.refresh_notes:
        updateView();
        break;

    case R.id.delete_notes:
        db.deleteAllNotes();
        updateView();
        break;
    }

    return true;

}

}

I have to shutdown the application for view the changes.

Someone have an idea ?

Was it helpful?

Solution

try to

private void updateView() 
{
  Cursor newCursor = db.getNotes();
  adapter.swapCursor(newCursor);
}

OTHER TIPS

if there is a change to your database you need to re-query and swap in the new cursor with the new data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top