我正在尝试制作一个带有翻译的测验应用程序。所以有两个数组列表,用不同的语言装载问题。
我可以在列表视图中显示它们。我的问题是,当我点击一个单选按钮时,我希望另一个单选按钮也被选中。
我使用一个ListView自定义布局与2个单选按钮。
这是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;
}
}
}
您的代码中没有复选框。我觉得应该改成Radio button。我不明白你为什么要选中两个单选按钮。无论如何,如果你分别设置这些,(不在RadioGroup中),那么你可以一次检查两个。
一个建议:将onClick侦听器写在if-else块之外