0

I have two fragments:AllItemsFragment, CreateItemDialogFragment

The AllItemsFragment displays a list of items in the sqlite,CreateItemDialogFragment is used to create the item to the SQLite.

Now I have a question is after I creating a item in createItemDialogFragment,the dialog dismiss, how to update the display of the items in AllItemsFragment

In 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;
        } 

In 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();


    }
}
Siyuan Hu
  • 23
  • 2
  • you can use interface as a callback to the hosting activity and then communicate from activity to fragment. read http://developer.android.com/training/basics/fragments/communicating.html – Raghunandan Mar 11 '16 at 06:47
  • refer this - http://stackoverflow.com/q/15313598/1501644 – yuva ツ Mar 11 '16 at 07:00

1 Answers1

0

Try to update the data on onResume callback of the AllItemsFragment.

@Override
public void onResume(){
    super.onResume();
    // update data over here.
}
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40