Issue
I was searching on Google and StackOverflow for any solution, and i found hundereds of them but none of them worked in my case.
I have 3 RadioButtons and as it should be only one is selectable.
Now i want know which Radio Button is selected when I push the "Visit" Button. My Function for the VisitButton is called but when i try to read the RadioButton the result is always the same.
It does not matter which one I select, it always chooses the default button.
I tried a variety of different approaches, onChangeCheckListener, onClickListener but none of them seem to work.
What am I doing wrong?
Here's my code-snippet:
MicrolabB = (RadioButton) findViewById(R.id.Microlab);
MoodleB = (RadioButton) findViewById(R.id.Moodle);
ISAB = (RadioButton) findViewById(R.id.ISA);
PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
VISIT = (Button) findViewById(R.id.Visit);
MicrolabB.setOnClickListener(this);
ISAB.setOnClickListener(this);
MoodleB.setOnClickListener(this);
public void VisitWebPage(View v){
if(v.getId()==R.id.Visit){
if(MicrolabB.isChecked())
ApduNFC.Portal =1;
if(ISAB.isChecked())
ApduNFC.Portal=2;
if(MoodleB.isChecked())
ApduNFC.Portal=3;
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
And my XML:
<RadioGroup
android:id="@+id/PortalSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" >
<RadioButton
android:id="@+id/Microlab"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text="Microlab"
android:clickable="true"
/>
<RadioButton
android:id="@+id/ISA"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="IS-A"
android:clickable="true"
android:checked="true" />
<RadioButton
android:id="@+id/Moodle"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:text="Moodle"
android:clickable="true" />
</RadioGroup>
<Button
android:id="@+id/Visit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="VisitWebPage"
android:text="Visit" />
Solution
Solved the Problem.
public void VisitWebPage(View v){
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
int ID = PortalSelect.getCheckedRadioButtonId();
if(v.getId()==R.id.Visit){
int rb1Id = MicrolabB.getId();
if(ID == MicrolabB.getId())
Log.d("RB Selected","1");
if(ID == ISAB.getId())
Log.d("RB Selected","2");
if(ID == MoodleB.getId())
Log.d("RB Selected","3");
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
After I put in the Line
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
in the VisitWebPage Function everything seems to work. I have no Idea why, because I defined the Radio Group before in this class. But I'm happy that it is working now.
Answered By - GhostofRazgriz
Answer Checked By - Willingham (JavaFixing Volunteer)