javaandroidxmlradio-buttonfindviewbyid

How to check radio button based on intent string


I'm trying to check a radio button based upon an value derived from an intent sent from the previous activity. Upon screen load, the radioButton.getText().toString() should compare against a specific value and check that particular radio button if it evaluates to true but I am not getting the expected results.

I've looked on stack overflow and various documentation/tutorial sites but I haven't came across anyone else having this particular issue. Can anyone point me in the right direction of solving this issue? Here's the relevant activity code :

courseStatus = getIntent().getStringExtra("course_status");
editCourseStatusG = findViewById(R.id.radioGroup1);
editStatusComplete = findViewById(R.id.editStatusCompleted);
editStatusDropped = findViewById(R.id.editStatusDropped);
editStatusOngoing = findViewById(R.id.editStatusOngoing);
editStatusPlanned = findViewById(R.id.editStatusPlanned);

    
if(courseStatus.equals(editStatusComplete.getText().toString())) {
        editStatusComplete.setChecked(true);
} else if (courseStatus.equals(editStatusDropped.getText().toString())) {
        editStatusDropped.setChecked(true);
} else if ((courseStatus.equals(editStatusOngoing.getText().toString()))) {
        editStatusOngoing.setChecked(true);
} else if ((courseStatus.equals(editStatusPlanned.getText().toString()))) {
        editStatusPlanned.setChecked(true);
}

XML code:

<RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <RadioButton
                    android:id="@+id/editStatusOngoing"
                    android:layout_width="match_parent"
                    android:text="Ongoing"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusCompleted"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Completed"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusDropped"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Dropped"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusPlanned"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Planned"
                    android:layout_weight="1"/>

            </RadioGroup>

Solution

  • After doing some troubleshooting, I still cannot find the answer to the issue at hand. The getText() method is not evaluating correctly to the given string. However, a work around it to compare directly the intent directly to a static string variable with the same text as the radioButton.getText() method should return.

    Working code:

           editCourseStatusG = findViewById(R.id.radioGroup1);
           editStatusComplete = findViewById(R.id.editStatusCompleted);
           editStatusDropped = findViewById(R.id.editStatusDropped);
           editStatusOngoing = findViewById(R.id.editStatusOngoing);
           editStatusPlanned = findViewById(R.id.editStatusPlanned);
    
           try {
               switch (courseStatus) {
                   case "Completed":
                       editStatusComplete.setChecked(true);
                       break;
                   case "Dropped":
                       editStatusDropped.setChecked(true);
                       break;
                   case "Ongoing":
                       editStatusOngoing.setChecked(true);
                       break;
                   case "Planned":
                       editStatusPlanned.setChecked(true);
                       break;
                   default:
                       editStatusPlanned.setChecked(true);
               }
           } catch(NullPointerException e) {
               editStatusPlanned.setChecked(true);
           }