2

Actually in my app when I swipe my RecyclerView it's open a custom AlertDialog if I'm doing operation with button's in it all is going as it have to be.

The issue comes when I press outside the AlertDialog because it's dismiss the AlertDialog without sending the notification to the recyclerView so the recyclerView is not turning back from the swipeMode.

How can I handle the click outside the AlertDialog? I just need to add in this handle exampleAdapter.notifyItemChanged.

Here is my AlertDialog code:

public void customAllertQuantity(final int position){

    AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);

    @SuppressLint("InflateParams") View mView = getLayoutInflater().inflate(R.layout.alert_quantity, null);

    final EditText quantita = mView.findViewById(R.id.editTextQTA);

    Button buttonPiu = mView.findViewById(R.id.btnPLUS);
    Button buttonMeno = mView.findViewById(R.id.btnMINUS);
    ImageButton save = mView.findViewById(R.id.saveButton);

    quantita.setText(String.valueOf(itemCassas.get(position).getQuant()));

    mBuilder.setView(mView);
    final AlertDialog dialog = mBuilder.create();
    dialog.show();

    buttonPiu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            quantita.setText(String.valueOf((Integer.parseInt(quantita.getText().toString()) + 1)));
        }
    });

    buttonMeno.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Integer.parseInt(quantita.getText().toString()) > 1){
                quantita.setText(String.valueOf((Integer.parseInt(quantita.getText().toString()) - 1)));
            }else{
                Log.i("LOG","QTA = 1");
                //
            }
        }
    });

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double prezz = itemCassas.get(position).getImporto();
            itemCassas.get(position).setQuant(Integer.parseInt(quantita.getText().toString()));
            itemCassas.get(position).setPrice(prezz *  itemCassas.get(position).getQuant());
            TotalPrice();
            exampleAdapter.notifyItemChanged(position);
            dialog.dismiss();

        }
    });


}
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • Possible duplicate of [How to dismiss the dialog with click on outside of the dialog?](https://stackoverflow.com/questions/8384067/how-to-dismiss-the-dialog-with-click-on-outside-of-the-dialog) – Gautam Surani Jun 20 '18 at 12:54

1 Answers1

3

There are two approaches, either disable cancelling or handle the dialog cancel ("click outside of dialog").

You can avoid dialog being cancelled as follows:

mBuilder.setView(mView);
mbuilder.setCancelable(false);
final AlertDialog dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);

Handle cancel action or "click outside of dialog":

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            //send notification to adapter
        }
    });

Note: The dialog may be dismissed for reasons other than being canceled or one of the supplied choices being selected. If you are interested in listening for all cases where the dialog is dismissed then use setOnDismissListener

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Actually i was yet trying to use the .setOnCancelListener but it wasn't working but i just fixed it by placing the listener before i was setting the layout. Thank you – NiceToMytyuk Jun 20 '18 at 12:43