0

I'm building an app with Android Studio.

I have an Actvity with some fragments. These fragments extends a BaseFragment Class In one of this Fragment I have a ListView with custon Adapter. I have implements a ClickListener of this ListView like this:

        lvMyResults = view.findViewById(R.id.lvMyResults);
        lvMyResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Food foodItem = (Food) parent.getAdapter().getItem(position);
                addFoodToMeal(foodItem);

            }
        });

The method addFoodToMeal, show at the user an Alert Dialog. Now I want to refresh the list view after then the user close this AlertDialog.

Can I intercept the close of Dialog from onItemClickListener?

bircastri
  • 2,169
  • 13
  • 50
  • 119

2 Answers2

0

If you want to close only by a button, you just have to call setCancelable(false) and handle your button. Else you can check this : How to handle AlertDialog close event?

KotC
  • 53
  • 2
  • 10
0

You can return dialog from addFoodToMeal() and set dismissListener on that :

Ex :

public AlertDialog addFoodToMeal(.....){

        //..... code to show alert dialog
        return alertDialog;

}

and use it like :

    lvMyResults = view.findViewById(R.id.lvMyResults);
    lvMyResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Food foodItem = (Food) parent.getAdapter().getItem(position);
            AlertDialog alertDIalog = addFoodToMeal(foodItem);
            if(alertDIalog!=null){

                alertDIalog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        // dialog dismissed
                        // update your listview' adapter here
                        if(lvAdapter!=null){
                            lvAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }

        }
    });
NehaK
  • 2,639
  • 1
  • 15
  • 31