Search code examples
androidcursorandroid-cursoradapter

Android using custom CursorAdapter with onContentChanged()


I'm still trying to make a good code by using a cursorAdapter on a listView.

I wish that on my activity, when I change the data in the cursor, it automatically update the cursor in the adapter, update the adapter and of course the listView.

Magic, there is this function : onContentChanged() which is a function of CursorAdapter that is called when the cursor of this adapter is "notified".

But I'm failing calling this function.

Now the interresting part of code (which doesn't work), if someone can tell me where I'm mistaken and what is my misunderstanding, I'll be thanksful.

Activity :

public class EverydayNotesAndroid3Activity extends Activity {
    /** Called when the activity is first created. */

    private Cursor cursorNotes;
    private NoteDataSource dataNoteConnector;
    private NotesCursorAdapter notesCursorAdapter;
    private InputMethodManager imm;
    private Activity activity;
    private ListView listView;
    private int positionItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        activity = this;

        dataNoteConnector = new NoteDataSource(activity);

        dataNoteConnector.open();

        cursorNotes = dataNoteConnector.getAllNotes();

        startManagingCursor(cursorNotes);

        listView = (ListView) findViewById(R.id.main_list);

        notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, 3);

        listView.setAdapter(notesCursorAdapter);

        Button b = new Button(activity);

        b = (Button) findViewById(R.id.done);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dataNoteConnector.createNoteTop("Hello stack overflow world");


            }
        });

CursorAdapter :

@Override


public void onContentChanged(){
        System.out.println("foux de fafa");
        this.notifyDataSetChanged();
    }

Let me remind you my problem is I want to call this function, but it is not called.

And the database class, but I don't know if it matters because the insertion on database is okay :

public Cursor create

NoteTop(String note) {

        idNoteMin++;
        ContentValues values = new ContentValues();
        values.put(DataBaseHelper.DATABASE_ID_NOTE, idNoteMin);
        values.put(DataBaseHelper.DATABASE_CONTENT_NOTE, note);
        long insertId = database.insert(DataBaseHelper.DATABASE_TABLE_NOTE, null,
                values);
        Cursor cursor = database.query(DataBaseHelper.DATABASE_TABLE_NOTE,
                allColumns, DataBaseHelper.DATABASE_ID_NOTE + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        return cursor;
    }

Ok so again, my 2 question :

1) How can I call the onContentChanged() function of my adapter (without of course explicitly calling it)

2) I wonder if my code is the good way to feed a listView with a custom cursorAdapter.

Thanks for paying attention.


Solution

  • It actually works the other way around, onContentChanged is called after the adapter is notified with notifyDataSetChanged.

    Try changing your button click listener to this:

    @Override
            public void onClick(View v) {
    
                dataNoteConnector.createNoteTop("Hello stack overflow world");
                notesCursorAdapter.notifyDataSetChanged();
    
            }