Reputation: 13
I want to get a value from checking a RadioButton. I have three RadioButton (with different values) inside a RadioGroup. Well, the first time I check a RadioButton I am able to get the value, but if I change to another RadioButton I cannot get values anymore. It is like if onCheckedChanged is only call the first time I check a RadioButton.
Any helpful will be appreciated. Thanks in advance.
This is my code:
public class AsesorNutri extends AppCompatActivity {
RadioGroup deporte;
RadioButton andar;
RadioButton correr;
RadioButton ciclismo;
String TAG="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asesor_nutri);
andar=(RadioButton)findViewById(R.id.andar);
andar.setChecked(true);
correr=(RadioButton)findViewById(R.id.correr);
ciclismo=(RadioButton)findViewById(R.id.ciclismo);
deporte = (RadioGroup)findViewById(R.id.deporte);
deporte.setOnCheckedChangeListener(cambiodeporte);
}
public RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch(i)
{
case R.id.andar:
Log.i(TAG, "cambia andar");
// do what you wan there
break;
case R.id.correr:
Log.i(TAG, "cambia correr");
break;
case R.id.ciclismo:
Log.i(TAG, "cambia correr");
break;
}
}
};
}
And this is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
tools:context="lnspad.fitnessup.AsesorNutri">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="24dp"
android:layout_marginTop="67dp"
android:text="Seleccione Deporte"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/deporte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="18dp"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3">
<RadioButton
android:id="@+id/andar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView3"
android:layout_alignTop="@+id/deporte"
android:text="Andar" />
<RadioButton
android:id="@+id/correr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correr"
android:layout_marginEnd="39dp"
android:layout_alignBaseline="@+id/ciclismo"
android:layout_alignBottom="@+id/ciclismo"
android:layout_toStartOf="@+id/ciclismo" />
<RadioButton
android:id="@+id/ciclismo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ciclismo"
android:layout_below="@+id/deporte"
android:layout_alignParentEnd="true"
android:layout_marginEnd="49dp" />
</RadioGroup>
</RelativeLayout>
Upvotes: 0
Views: 528
Reputation: 28793
You code works. I changed a bit and showed toasts.
private RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch (i) {
case R.id.andar:
Log.i(TAG, "cambia andar");
Toast.makeText(MainActivity.this, "1. cambia andar", Toast.LENGTH_SHORT).show();
break;
case R.id.correr:
Log.i(TAG, "cambia correr");
Toast.makeText(MainActivity.this, "2. cambia correr", Toast.LENGTH_SHORT).show();
break;
case R.id.ciclismo:
Log.i(TAG, "cambia ciclismo");
Toast.makeText(MainActivity.this, "3. cambia ciclismo", Toast.LENGTH_SHORT).show();
break;
}
}
};
Maybe it's a device problem.
Upvotes: 0
Reputation: 986
Your code is odd. Not sure why you have public access modifier on the listener. Why not define it the same way as you have the other variables? The annotation claims it's a resource ID, and it's constant, so it has to work. Otherwise, it could be a lifecycle issue because you're initializing in onCreate().
EG:
RadioGroup deporte;
RadioButton andar;
RadioButton correr;
RadioButton ciclismo;
RadioGroup.OnCheckedChangeListener cambiodeporte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_asesor_nutri);
andar=(RadioButton)findViewById(R.id.andar);
andar.setChecked(true);
correr=(RadioButton)findViewById(R.id.correr);
ciclismo=(RadioButton)findViewById(R.id.ciclismo);
deporte = (RadioGroup)findViewById(R.id.deporte);
cambiodeporte = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
switch(i)
{
case R.id.andar:
Log.i(TAG, "cambia andar");
// do what you wan there
break;
case R.id.correr:
Log.i(TAG, "cambia correr");
break;
case R.id.ciclismo:
Log.i(TAG, "cambia correr");
break;
}
}
};
deporte.setOnCheckedChangeListener(cambiodeporte);
}
Upvotes: 0