我有两个活动 – 一个显示SQLite的数据,另一个在那里添加数据.我可以用标签在它们之间切换.问题是,如果我添加一个新项目,我不知道如何刷新列表,因为它在不同的活动类中,我无法在我的添加类中访问它.怎么解决这个问题?
这是我的两个班级:
列表:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
final ShoppingSQL shoppingSQL = new ShoppingSQL(this);
final List list = shoppingSQL.getAll();
final ArrayAdapter adapter = new ArrayAdapter(
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类,然后再使用新项填充列表.
标签:android,sqlite
来源: https://codeday.me/bug/20190901/1782417.html