0

I have a custom dialog box that I am displaying in the following way:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dict_add_word_dialog_box);
    ok = findViewById(R.id.dictDialog_confirmButton);
    cancel = (Button) findViewById(R.id.dictDialog_cancelButton);
    ok.setOnClickListener(this);
    cancel.setOnClickListener(this);
}

This displays when tapping the Floating Action Button, via:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this);
            customDialog.show();
            refreshRecyclerView();
        }
    });

I'd like the refreshRecyclerView(); to run only once the user has pressed the OK button on the dialog box. How would I go about doing this?

In addition, how would I go about running it only if the user has pressed OK, and not Cancel?

Harry Adams
  • 423
  • 5
  • 16
  • You will find here an answer, https://stackoverflow.com/a/15020949/3460038 – machichiotte Dec 03 '18 at 16:00
  • There are some event listeners for Dialogs. See this: [https://stackoverflow.com/questions/21438100/how-to-handle-alertdialog-close-event?rq=1](https://stackoverflow.com/questions/21438100/how-to-handle-alertdialog-close-event?rq=1) – Giovanne Dec 03 '18 at 16:00

2 Answers2

1

Create a runnable with your refreshRecyclerView method in it:

Runnable r = new Runnable() {
        @Override
        public void run() {
            refreshRecyclerView();
        }
    }

then create a handler for that runnable:

Handler handler = new Handler();

inside your onClickListener for the ok button trigger the runnable by calling the following:

handler.post(r);
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • The OK button and Cancel button use the same onClickListener with a switch/case to check the button. Is there a way to cancel the runnable if they press Cancel? – Harry Adams Dec 03 '18 at 16:04
  • You can cancel it by using the following handler.removeCallbacks(r); – Nikos Hidalgo Dec 03 '18 at 16:06
  • Also, the onClickListener is in one class whilst the refreshRecyclerView is in a separate activity - how can I call this function as it's public? Sorry if that's a stupid question, I'm fairly unfamiliar – Harry Adams Dec 03 '18 at 16:08
  • I would have to see your code to know for sure, but generally, you can make a method public static, and in the activity you want to use it, you create an object of the class that contains it, and use it like so object.nameOfMethod(); Another way is to use an interface and have it as a callback method. If you're having problems, try creating a new question so that more people can see the specifics, as this current thread is irrelevant – Nikos Hidalgo Dec 03 '18 at 16:12
0

Why not add a listener to your custom dialog?

var listener: Listener

public interface Listener {
    void onPositiveActionPressed();
}

Then in your custom dialog's ok.onClickListener you'd have the below;

if(listener != null) {
    listener.onPositiveActionPressed();
}

Finally;

DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this)
customDialog.listener = self

Of course having implemented DictCustomDialogBoxClass.Listener

Daniel T-D
  • 665
  • 6
  • 8