当前位置:首页 > 数据库 > SQlite

android – 在其他活动中添加new后刷新SQLite列表

我有两个活动 – 一个显示SQLite的数据,另一个在那里添加数据.我可以用标签在它们之间切换.问题是,如果我添加一个新项目,我不知道如何刷新列表,因为它在不同的活动类中,我无法在我的添加类中访问它.怎么解决这个问题?

这是我的两个班级:

列表:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);

    final ShoppingSQL shoppingSQL = new ShoppingSQL(this);
    final List<ShoppingData> list = shoppingSQL.getAll();

    final ArrayAdapter<ShoppingData> adapter = new ArrayAdapter<ShoppingData>(
            this, android.R.layout.simple_list_item_1, list);
    setListAdapter(adapter);

    this.getListView().setLongClickable(true);
       this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                shoppingSQL.delete((int)list.get(position).getId());
                adapter.remove(list.get(position));
                adapter.notifyDataSetChanged();
                return true;
            }
        });
}

加:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
}

public void ButtonOnClick(View v) {
    ShoppingSQL shoppingSQL = new ShoppingSQL(this);

    ShoppingData data = new ShoppingData();
    data.setName("test");

    shoppingSQL.add(data);

    Dialog d = new Dialog(this);
    d.setTitle("Added!");
    d.show();
}

我也有一点问题.在我的第一个(列表)活动中,当我在“onLongClick”中访问它们时,Eclipse使每个变量都成为最终变量 – 为什么这样可以避免?此外,对我应该注意什么以及使我的代码更好或者我做的任何其他错误的任何评论都会非常好.

解决方法:

我会保持简单.只需将Add class中的intent发送到List类,然后再使用新项填充列表.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!