Hi everybody, I need your help because i'm stuck in a problem! I have 4 radiobuttons that shows respectively 4 different images. Now i need to change those images after a selected spinner. I used this code but doesn't work properly because the listener takes only the first radio button and nothing else. Do you know how help me? I know there's something wrong..maybe i should save them into an array?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String stato = spinner.getItemAtPosition(position).toString();
if(stato.equals("Grams"))
{
rb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(rb1.isChecked()) {
l5.removeAllViews();
LayoutInflater inflater = (LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Porzioneuno pu =(Porzioneuno)inflater.inflate(R.layout.porzioneuno, null);
//l5.removeView(rl);
l5.addView(pu);
l5.setVisibility(View.VISIBLE);
//ImageView image = (ImageView) l5.findViewById(R.id.ImageView01);
//image.setBackgroundResource(R.drawable.centog);
}
}
});
rb2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(rb2.isChecked()) {
l5.removeAllViews();
LayoutInflater inflater = (LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Porzionedue pu2 =(Porzionedue)inflater.inflate(R.layout.porzionedue, null);
//l5.removeView(rl);
l5.addView(pu2);
l5.setVisibility(View.VISIBLE);
}}
});
rb3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(rb3.isChecked()) {
l5.removeAllViews();
LayoutInflater inflater = (LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Porzionetre pu3 =(Porzionetre)inflater.inflate(R.layout.porzionetre, null);
l5.addView(pu3);
l5.setVisibility(View.VISIBLE);
}
}
});
rb4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(rb4.isChecked()) {
l5.removeAllViews();
LayoutInflater inflater = (LayoutInflater)v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Porzionequattro pu4 =(Porzionequattro)inflater.inflate(R.layout.porzionequattro, null);
//l5.removeView(rl);
l5.addView(pu4);
l5.setVisibility(View.VISIBLE);
}
}
});
}
if (stato.equals("Ounces"))
{
if(rb1.isChecked()){
ImageView image = (ImageView) l5.findViewById(R.id.ImageView01);
image.setBackgroundResource(R.drawable.setteoz);
}
if(rb2.isChecked() ){
ImageView image = (ImageView) l5.findViewById(R.id.ImageView01);
image.setBackgroundResource(R.drawable.setteoz);
}
if(rb3.isChecked()){
ImageView image = (ImageView) l5.findViewById(R.id.ImageView01);
image.setBackgroundResource(R.drawable.diecioz);
}
if(rb4.isChecked()){
ImageView image = (ImageView) l5.findViewById(R.id.ImageView01);
image.setBackgroundResource(R.drawable.quattorz);
}
}
}