【发布时间】:2018-10-11 19:04:55
【问题描述】:
我在我称为 parentLinearLayout 的 LinearLayout 中为应用程序动态生成单选按钮(例如:3 个单选按钮)。问题是,它们不会自动进入一个组,所以如果我检查一个按钮然后另一个,都检查而不是取消选中第一个。所以,我想我应该做一个 RadioGroup。
该应用本身是一个问卷调查应用,每个问题都有不同数量的可能答案。我从单选按钮布局创建作为单选按钮的答案数量。
问题是,我收到一条错误消息,提示我应该先删除旧视图,但如果这样做,我会丢失生成的线性布局:The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的代码:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
for (int i=0;i<drawables;i++){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.radiobutton, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); <== HERE IS THE PROBLEM
rd.setText(current.getAnswers().get(i).getAns());
}
}
有什么想法可以解决这个问题吗?谢谢
【问题讨论】: