首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刷新ListActivity

刷新ListActivity
EN

Stack Overflow用户
提问于 2013-12-12 14:30:27
回答 2查看 73关注 0票数 0

我试着做一个Notes应用程序,但是当我用我制作的编辑器添加一个注释时,ListActivity没有更新。在数据库中添加一些内容之后,我尝试刷新我的activityList,但是它不起作用。这里我的代码:

代码语言:javascript
复制
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;

}

}

我必须关闭应用程序查看更改。

有人有主意吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-12 14:41:28

试着

代码语言:javascript
复制
private void updateView() 
{
  Cursor newCursor = db.getNotes();
  adapter.swapCursor(newCursor);
}
票数 1
EN

Stack Overflow用户

发布于 2013-12-12 14:32:03

如果您的数据库发生了更改,则需要在新的游标中重新查询和交换新数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20546217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档