在OnActivityResult中更新ExpandableListView

时间:2014-07-11 10:03:01

标签: android expandablelistview expandablelistadapter

在我的片段中,我有一个ExpandableListView,它将Categories作为组,Base_Items作为子项,它们来自数据库。从这里我打开一个Intent for result来输入新的项目。 OnActivityResult我希望新项目立即出现在ExpandableListView中,然后将其输入到DB。我试过了:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK){
        switch(requestCode)
        {
        case Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM:
            String new_item_name = data.getExtras().getString("ITEM_NAME");
            int new_item_category = data.getExtras().getInt("ITEM_CATEGORY");

            Base_Item bi = new Base_Item(-1, new_item_category, new_item_name);
            ArrayList<Base_Item> arr_bi = arr_all_categories.get(new_item_category).getCategory_items();
            arr_bi.add(bi);
            adapter.notifyDataSetChanged();
            manager.add_new_base_item(bi);
            break;
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

新项目正在插入数据库中,但ListView没有更新。我必须退出应用程序并重新打开它以查看列表中的新项目。

如何更新ExpandableListView并立即查看新项?谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我最终只是在我的onActivityResult方法中重新填充了ListView。我不确定它是最好的方式(谈到性能),也许我应该一直在寻找答案,但现在它才有效。