我嘗試做一個應用程序的筆記,但是當我添加一個筆記與我做的編輯器,但ListActivity不更新。 我嘗試在添加數據庫中的某些內容後刷新我的activityList,但它不起作用。在這裏我的代碼:刷新一個ListActivity
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;
}
}
我不得不關閉應用程序以查看更改。
有人有想法嗎?
既然你用光標的工作和你將數據插入到數據庫中需要'cursor.requery()'; – Sajmon
Requery()是不推薦使用的方法 –