从对话框片段传递到片段

时间:2016-03-11 06:40:47

标签: android fragment android-dialogfragment

我有两个片段:AllItemsFragment,CreateItemDialogFragment

AllItemsFragment显示sqlite中的项目列表,CreateItemDialogFragment用于为SQLite创建项目。

现在我有一个问题是在createItemDialogFragment中创建项目后,对话框关闭,如何更新AllItemsFragment中项目的显示

在AllItemsFragment中:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.d(LIFETAG, "onCreateView");
            View view = inflater.inflate(R.layout.fragment_all_items, container, false);

            ButterKnife.bind(this, view);
            tvTitle.setText("All Items");
            DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
            ArrayList<Item> items = dbHandler.getAllItems();

            AllItemsAdapter adapter = new AllItemsAdapter(getActivity(),items);

            lvItems.setAdapter(adapter);
            return view;
        } 

在CreateItemDialogFragment中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    View view =inflater.inflate(R.layout.fragment_create_item_dialog,null);

    ButterKnife.bind(this, view);
    itemPrice.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
    return view;
}


@OnClick(R.id.clear)
void clearDialog(){
    dismiss();
}


//create item
@OnClick(R.id.save)
void saveItem() {

   if(!itemName.getText().equals(null)||!itemPrice.getText().equals(null)){
        String name = itemName.getText().toString();

        String priceStr = itemPrice.getText().toString();
        Double price = Double.parseDouble(priceStr);
        Item item  = new Item(name,price);
        DbHandler dbHandler = new DbHandler(getActivity(),null,null,1);
        dbHandler.addItems(item);
        dismiss();


    }
}

1 个答案:

答案 0 :(得分:0)

尝试更新onResume的{​​{1}}回调数据。

AllItemsFragment