hi, i have some groups of radio buttons that i create programmatically. they work fine (please see code below). but calling radioGroup.getCheckedRadioButtonId() always returns -1 when it's called from onCheckedChanged(). is this correct behaviour?

thanks

package tayek.radio;
import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.CompoundButton.*;
public class RadioActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
questions=4;
LinearLayout main=createMainLayout();
setContentView(main);
linearLayouts=new LinearLayout[questions];
radioGroups=new RadioGroup[questions];
for(int i=0;i<questions;i++)
linearLayouts[i]=addTextViewAndRadioGroupToPopupLayout(i);
}
private LinearLayout createMainLayout() {
LinearLayout main=new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
buttons=new Button[questions];
textViews=new TextView[questions];
for(int i=0;i<questions;i++) {
textViews[i]=new TextView(this);
textViews[i].setText("Question "+(i+1));
main.addView(textViews[i]);
buttons[i]=new Button(this);
buttons[i].setText("Make a choice "+(i+1));
main.addView(buttons[i]);
buttons[i].setId(i);
buttons[i].setOnClickListener(buttonOnClickListener);
}
return main;
}
private void createPopupWindow(LinearLayout linearLayout) {
popupWindow=new PopupWindow(this);
popupWindow.setContentView(linearLayout);
popupWindow.setFocusable(true);
popupWindow.setWidth(480);
popupWindow.setHeight(640);
}
private LinearLayout addTextViewAndRadioGroupToPopupLayout(int question) {
LinearLayout linearLayout=new LinearLayout(this);
TextView textView=new TextView(this);
textView.setText("Question "+question);
linearLayout.addView(textView);
int answers=3;
RadioButton[] radioButtons=new RadioButton[answers];
radioGroups[question]=addRadioButtonsToGroup(question,radioButtons);
linearLayout.addView(radioGroups[question]);
linearLayout.setId(question);
radioButtons[0].setChecked(true);
return linearLayout;
}
static int[] colors=new int[]{0xff0000,0x00ff00,0x0000ff};
private RadioGroup addRadioButtonsToGroup(int question,RadioButton[] radioButtons) {
RadioGroup radioGroup=new RadioGroup(this);
for(int i=0;i<radioButtons.length;i++) {
radioButtons[i]=new RadioButton(this);
radioButtons[i].setText("Question "+(question+1)+" Choice "+(i+1));
radioButtons[i].setId(i);
radioButtons[i].setBackgroundColor(0xff0000/* colors[i%colors.length] */);
radioButtons[i].setOnCheckedChangeListener(onCheckedChangeListener);
radioButtons[i].setOnClickListener(radioButtonOnClickListener);
radioGroup.addView(radioButtons[i]);
}
radioGroup.setId(question);
return radioGroup;
}
int questions;
LinearLayout[] linearLayouts;
Button[] buttons;
RadioGroup[] radioGroups;
TextView[] textViews;
PopupWindow popupWindow;
OnCheckedChangeListener onCheckedChangeListener=new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(popupWindow!=null) popupWindow.dismiss();
RadioButton radioButton=(RadioButton)buttonView;
int a=radioButton.getId();
ViewParent viewParent=buttonView.getParent();
RadioGroup radioGroup=(RadioGroup)viewParent;
int q=radioGroup.getId();
if(isChecked) {
buttons[q].setText(radioButton.getText());
// recalculate the score
int a2=radioGroup.getCheckedRadioButtonId();
if(a!=a2) {
System.err.println("CompoundButton id="+a);
System.err.println("radioGroup.getCheckedRadioButtonId()="+a2);
}
}
}
};
OnClickListener buttonOnClickListener=new OnClickListener(){
public void onClick(View v) {
System.out.println("button "+v.getId()+" clicked at "+new Date());
createPopupWindow(linearLayouts[v.getId()]);
popupWindow.showAtLocation(v,Gravity.CENTER,20,20);
// popupWindow.showAsDropDown(v);
}
};
OnClickListener radioButtonOnClickListener=new OnClickListener(){
public void onClick(View v) {
System.out.println("radio button "+v.getId()+" clicked at "+new Date());
if(popupWindow!=null) popupWindow.dismiss();
}
};
}

---
co-chair http://ocjug.org/

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to