我尝试做一个Notes的应用程序但是当我用编辑器添加一个注释但是ListActivity没有更新。 我在数据库中添加一些内容之后尝试刷新我的activityList但它不起作用。这是我的代码:
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;
}
}
我必须关闭应用程序以查看更改。
有人有想法吗?
答案 0 :(得分:1)
如果您的数据库发生了变化,您需要重新查询并使用新数据交换新光标
答案 1 :(得分:1)
尝试
private void updateView()
{
Cursor newCursor = db.getNotes();
adapter.swapCursor(newCursor);
}