Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid using custom CursorAdapter with onContentChanged()
    text
    copied!<p>I'm still trying to make a good code by using a cursorAdapter on a listView.</p> <p>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.</p> <p>Magic, there is this function : onContentChanged() which is a function of CursorAdapter that is called when the cursor of this adapter is "notified". </p> <p>But I'm failing calling this function.</p> <p>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.</p> <p>Activity :</p> <pre><code>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"); } }); </code></pre> <p>CursorAdapter : </p> <pre><code>@Override public void onContentChanged(){ System.out.println("foux de fafa"); this.notifyDataSetChanged(); } </code></pre> <p>Let me remind you my problem is I want to call this function, but it is not called.</p> <p>And the database class, but I don't know if it matters because the insertion on database is okay :</p> <pre><code>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; } </code></pre> <p>Ok so again, my 2 question :</p> <p>1) How can I call the onContentChanged() function of my adapter (without of course explicitly calling it)</p> <p>2) I wonder if my code is the good way to feed a listView with a custom cursorAdapter.</p> <p>Thanks for paying attention.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload