0

I was doing the Android Developer Fundamentals Course when I had the following doubt in the below code.


public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        // Check which radio button was clicked.
        switch (view.getId()) {
            case R.id.sameday:
                if (checked)
                    // Same day service
                    displayToast(getString(R.string.same_day_messenger_service));
                break;
            case R.id.nextday:
                if (checked)
                    // Next day delivery
                    displayToast(getString(R.string.next_day_ground_delivery));
                break;
            case R.id.pickup:
                if (checked)
                    // Pick up
                    displayToast(getString(R.string.pick_up));
                break;
            default:
                // Do nothing.
                break;
        }
}

My doubt is we could just have used the view variable to understand which radiobutton was clicked since view.getId() returns the id of the selected radio button. At the moment the code uses isChecked() and view.getId() both.

Muhamed El-Banna
  • 593
  • 7
  • 21
lordzeus1989
  • 55
  • 1
  • 7

1 Answers1

0

It appears to be pointless. checked is always true.

Andrew G
  • 2,596
  • 2
  • 19
  • 26