我试着做一个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;
}
}我必须关闭应用程序查看更改。
有人有主意吗?
发布于 2013-12-12 14:41:28
试着
private void updateView()
{
Cursor newCursor = db.getNotes();
adapter.swapCursor(newCursor);
}发布于 2013-12-12 14:32:03
如果您的数据库发生了更改,则需要在新的游标中重新查询和交换新数据。
https://stackoverflow.com/questions/20546217
复制相似问题