我正在努力制作一个带翻译的测验应用程序。因此,有两个不同语言的问题加载的ArrayLists。
我也可以在列表视图中显示它们。我的问题是,当我点击One Radiobutton时,我希望其他Radiobutton也被选中。
我正在使用带有2个Radiobuttons的自定义布局的ListView。
这是我的ListView的ArrayAdater
private class RBAdapterClass extends ArrayAdapter<cOptions> {
private ArrayList<cOptions> fOptions;
private ArrayList<cOptions> lOptions;
private ArrayList<Boolean> mChecked;
private int mfSelectedPosition = -1;
private RadioButton mfSelectedRB;
private int mlSelectedPosition = -1;
private RadioButton mlSelectedRB;
public RBAdapterClass(Context context, int textViewResourceId, ArrayList<cOptions> lOpt, ArrayList<cOptions> fOpt)
{
super(context,textViewResourceId,fOpt);
this.fOptions = new ArrayList<cOptions>();
this.fOptions.addAll(fOpt);
this.lOptions = new ArrayList<cOptions>();
this.lOptions.addAll(lOpt);
mChecked = new ArrayList<Boolean>();
}
class ViewHolder {
TextView ForeignCode;
RadioButton ForeignName;
TextView LocalCode;
RadioButton LocalName;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holderf ;
Log.v("Convert View",String.valueOf(position));
if(convertView==null){
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.activity_activity_rb,null);
holderf=new ViewHolder();
holderf.LocalCode = (TextView) convertView.findViewById(R.id.codeLocal);
holderf.LocalName = (RadioButton)convertView.findViewById(R.id.radioBLocal);
holderf.ForeignCode = (TextView) convertView.findViewById(R.id.codeForeign);
holderf.ForeignName = (RadioButton)convertView.findViewById(R.id.radioBForeign);
convertView.setTag(holderf);
holderf.ForeignName.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
RadioButton rb = (RadioButton) v ;
if(position != mfSelectedPosition && mfSelectedRB!=null )
{
mfSelectedRB.setChecked(false);
if(mlSelectedRB!=null)
{
mlSelectedRB.setChecked(false);
}
}
mfSelectedPosition = position;
mfSelectedRB = (RadioButton)v;
cOptions optSelected = (cOptions) rb.getTag();
Toast.makeText(getApplicationContext(),"Clicked on RadioButton: " + rb.getText() +" is " + rb.isChecked(),
Toast.LENGTH_LONG).show();
optSelected.setSeleted(rb.isChecked());
holderf.ForeignName.setChecked(rb.isChecked());
holderf.LocalName.setChecked(rb.isChecked());
}
});
holderf.LocalName.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
if(position != mlSelectedPosition && mlSelectedRB!=null)
{
mlSelectedRB.setChecked(false);
if(mfSelectedRB!=null)
{
mfSelectedRB.setChecked(false);
}
}
mlSelectedPosition = position;
mlSelectedRB = (RadioButton)v;
RadioButton rb = (RadioButton) v ;
cOptions optSelected = (cOptions) rb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on RadioButton: " + rb.getText() +" is " + rb.isChecked(),Toast.LENGTH_LONG).show();
optSelected.setSeleted(rb.isChecked());
holderf.ForeignName.setChecked(rb.isChecked());
holderf.LocalName.setChecked(rb.isChecked());
}
});
}
else
{
holderf = (ViewHolder) convertView.getTag();
}
cOptions optfSelected = fOptions.get(position);
cOptions optlSelected = lOptions.get(position);
if(mlSelectedPosition!=position)
{
holderf.ForeignCode.setText(" (" + optfSelected.getsOptionsText() + ")");
holderf.ForeignName.setText(optfSelected.getsOptionsText());
holderf.ForeignName.setChecked(optfSelected.getSeleted());
holderf.ForeignName.setTag(optfSelected);
holderf.LocalCode.setText("("+optlSelected.getsOptionsText()+")");
holderf.LocalName.setText(optlSelected.getsOptionsText());
holderf.LocalName.setChecked(optlSelected.getSeleted());
holderf.LocalName.setTag(optlSelected);
}
else
{
holderf.ForeignCode.setText(" (" + optfSelected.getsOptionsText() + ")");
holderf.ForeignName.setText(optfSelected.getsOptionsText());
holderf.ForeignName.setChecked(optfSelected.getSeleted());
holderf.ForeignName.setTag(optfSelected);
holderf.LocalCode.setText("("+optlSelected.getsOptionsText()+")");
holderf.LocalName.setText(optlSelected.getsOptionsText());
holderf.LocalName.setChecked(optlSelected.getSeleted());
holderf.LocalName.setTag(optlSelected);
}
return convertView;
}
}
}
答案 0 :(得分:0)
您的代码中没有复选框。认为它将是单选按钮。我不明白你为什么要检查两个单选按钮。无论如何,如果你单独设置它们(不在 RadioGroup 中),那么你可以一次检查两个。
一个建议:在if-else块之外编写onClick侦听器