0

在我的一项活动中,我有EditText,提交Button和一个ListView. 的数据ListView是从数据库中检索的。为了从数据库中检索数据并适应ListView我使用了以下代码。

private void loadList() {
    mylist = new ArrayList<HashMap<String, Object>>();
    mylist.clear();
    List<Data> catDesc = dbhelper.getCatMasterDesc(id);
    ArrayAdapter<Data> adapter = new SimpleAdapter(getApplicationContext(), R.layout.list, catDesc);
    lv1 = (ListView) findViewById(R.id.masListView1);
    lv1.setAdapter(adapter);
}

每当我更新数据库以显示更新的列表视图时,我都会调用此方法。

String str = category.getText().toString();
yes.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
    dbhelper.UpdateMasterDesc(str);
    loadList(); // see here.
}
});

这就是我更新我的ListView. 我认为这不是一个好方法。如果是意味着请建议我如何更新我的ListView每当我更新数据库时。

谢谢你。

4

1 回答 1

0

notifyDataSetchanged() 是 Raghunandan 提出的答案。

但在调用它之前,您需要更新已包含在 catDesc 列表中的对象数据。

从你的代码来看,我相信什么

dbhelper.UpdateMasterDesc(str);

是添加一个新类别吗?如果是,请执行

catDesc.add(new Data(whatever you need to declare it with str))

在调用 notifyDataSetChanged() 之前。

于 2013-05-04T05:21:26.853 回答