Is this the most efficient way to set listeners for my RadioGroup?

23 Views Asked by At

I have a DialogView that will pop up when the user click a button, and inside this DialogView I have a RadioGroup. I want to set a listener to my RadioGroup, however I can't help but to think that my setRadioGroupOnClickListener will get called everytime that I press the button to create the DialogView, which seems a bit ineffecient to me...Is it possible that I can have the listeners set up so that it will only be called once in the applications entire life cycle?

Here is what I have as of now and it works

@Override
protected void showInputDialog() {

    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.adjective_input_layout, null);
    dialogBuilder.setView(dialogView);
    dialogBuilder.setTitle("Create new adjective flashcard");
    AlertDialog dialog = dialogBuilder.create();
    ...

    setRadioGroupOnClickListener(dialogView);

    dialog.show();
}

private void setRadioGroupOnClickListener(View v) {
    RadioGroup radioGroup = v.findViewById(R.id.adjective_translate_radio_group);
    radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
        switch (checkedId) {
            case R.id.adjective_manual_translation:
                //do stuff
                break;
            case R.id.adjective_eng_auto_translation:
                //do some other stuff
                break;
            case R.id.adjective_swe_auto_translation:
                // do some other other stuff
                break;
        }
    });
}

OR is it better to just set an onClick listener in my layout for all three of these items?

0

There are 0 best solutions below