我有一个自定义 cursorAdapter 肯定缺少一些东西,因为当我在数据库中添加一个项目时,它不会显示在 listView 上,但是如果我重新启动应用程序,它会显示添加的行。
这是我的活动和我的 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();
}
});
}
感谢您的关注。
编辑:我找到了解决方案,但我讨厌它,我希望有其他解决方案。
现在我有我的活动:
cursorNotes = dataNoteConnector.createNoteTop("American Alex is god");
cursorNotes = dataNoteConnector.getAllNotes();
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);
listView.setAdapter(notesCursorAdapter);
我觉得这很疯狂,更新后应该有一种更简单的方法来更新列表。在这里,我正在重新创建一个游标,重新创建一个我用新游标提供的 cursorAdapter,然后将此 cursorAdapter 影响到 listView。这是来自 android doc 的记事本示例,但我确信有一种方法可以通过操纵光标来更新适配器和 listView,但我不够聪明,无法弄清楚。