Reputation: 2150
I have a custom cursorAdapter that is certainly missing something, because when I add a item in the database, it isn't shown on the listView, but if I relaunch the application, it show added rows.
Here are my activity and my customAdapter :
package com.android.adapter;
import com.android.activity.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class NotesCursorAdapter extends CursorAdapter{
private Context context;
private static final int TYPE_ITEM = 0;
private static final int TYPE_ADD_NOTE_BOTTOM = 1;
private static final int TYPE_ADD_NOTE_TOP = 2;
private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1;
public NotesCursorAdapter (Context context, Cursor cursor, boolean flag){
super(context, cursor, flag);
this.context = context;
}
@Override
public void bindView(View v, Context con, Cursor cursor) {
TextView content = (TextView)v.findViewById(R.id.note);
content.setText(cursor.getString(cursor.getColumnIndex("content_note")));
}
@Override
public View newView(Context con, Cursor cursor, ViewGroup vp) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.row_note, vp, false);
bindView(v, context, cursor);
return v;
}
}
package com.android.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.android.adapter.NotesCursorAdapter;
import com.android.database.NoteDataSource;
public class EverydayNotesAndroid3Activity extends Activity {
/** Called when the activity is first created. */
private Cursor cursorNotes;
private NoteDataSource dataNoteConnector;
private NotesCursorAdapter notesCursorAdapter;
private Activity activity;
private ListView listView;
@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, true);
listView.setAdapter(notesCursorAdapter);
Button b = new Button(activity);
b = (Button) findViewById(R.id.done);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cursorNotes = dataNoteConnector.createNoteTop("American Alex is god");
cursorNotes.requery();
System.out.println("and everyone should listen to him");
// notesCursorAdapter.changeCursor(cursorNotes); // automatically closes old Cursor
// notesCursorAdapter.notifyDataSetChanged();
}
});
}
Thanks for paying attention.
EDIT : I've found a solution but I hate it, I wish there were an other solution.
Now I have in my activity :
cursorNotes = dataNoteConnector.createNoteTop("American Alex is god");
cursorNotes = dataNoteConnector.getAllNotes();
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);
listView.setAdapter(notesCursorAdapter);
I find it insane, there should be a simpler way to update the list after an update. Here, I'm recreating a cursor, recreating a cursorAdapter that I feed with the new cursor, an then affect this cursorAdapter to the listView. This is as the notepad example from android doc, but I'm sure there is a way to update the the adapter and the listView just by manipulating the cursor, but I'm not smart enough to figure it out.
Upvotes: 0
Views: 856
Reputation: 2605
try un commenting the last line in your activity
adapter.notifydatasetcanged();
Upvotes: 1
Reputation: 13552
This is what Google has to say about requery()
This method is deprecated. Don't use this. Just request a new cursor, so you can do this asynchronously and update your list view once the new cursor comes back.
You can check it out here. http://developer.android.com/reference/android/database/Cursor.html
So they actually suggest you do what you have done. :) But if you still want to do it the other way, uncomment the notesCursorAdapter.notifyDataSetChanged(). Ideally requery() is supposed to call the noifydatasetchange(), but I have observed that an explicit call is required to refresh the list.
Upvotes: 0
Reputation: 8852
well i won't recommend @Aashish solution. the best thing would be to make you listview and adapter member variables and then recreate adapter
private ListView listView;
private ListAdapter notesCursorAdapter;
private Cursor cursorNotes;
private void update() {
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);
listView.setAdapter(notesCursorAdapter);
}
Upvotes: 0