我正在尝试制作一个带有翻译的测验应用程序。因此,有两个以不同语言加载问题的 ArrayList。
我也可以在列表视图中显示它们。我的问题是当我单击一个单选按钮时,我希望另一个单选按钮也被选中。
我正在使用带有 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;
}
}
}