1、问题描述
在listview_detetion.xml文件中放置了两个RadioButton,代码如下所示:
<RadioGroup
android:id="@+id/rb_detection_group"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_detection_1_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/floralwhite"
android:text="合格"
android:textSize="15sp" />
<RadioButton
android:id="@+id/rb_detection_2_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/floralwhite"
android:text="不合格"
android:textSize="15sp" />
</RadioGroup>
利用Activity中listview的适配器让其在ListView中的每一项都可以显示,但是在实际测试中发现,当listview有多条记录时,点击前面的RadioButton时,下面的某一项也会随着上面的点击事件,显示一样的选择项。
2、解决方案
经过查看资料,发现了一篇文章,测试后发现解决了之前的问题。文章地址:地址
自己Adapter和ViewHolder的代码如下:
class DetectionAdapter1 extends BaseAdapter{
private Map<Integer,Integer> hashMap = new HashMap<>();// key封装的是它爹的tag值,value封装儿子radiobutton
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
//如果没有复用的
if(convertView == null){
//加载item的布局
convertView = View.inflate(DetctionActivity.this, R.layout.listview_detection1, null);
viewHolder = new ViewHolder();
viewHolder.contentView= (TextView) convertView.findViewById(R.id.tv_detection_item1);
viewHolder.rb_detection_1 = convertView.findViewById(R.id.rb_detection_1_1);
viewHolder.rb_detection_2 = convertView.findViewById(R.id.rb_detection_2_2);
viewHolder.rg_detection_group = convertView.findViewById(R.id.rb_detection_group);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.rg_detection_group.setTag(position);//给RadioGroup 弄个tag标记
if(hashMap.containsKey(position))
{
viewHolder.rg_detection_group.check(hashMap.get(position));
}
else
{
viewHolder.rg_detection_group.clearCheck();
}
viewHolder.rg_detection_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if ((Integer)group.getTag() == position){
boolean b = false;
if (checkedId == R.id.rb_detection_1_1){
b = true;
hashMap.put((Integer) group.getTag(),R.id.rb_detection_1_1);
}else if (checkedId == R.id.rb_detection_2_2){
b = true;
hashMap.put((Integer) group.getTag(),R.id.rb_detection_2_2);
}
}
}
});
viewHolder.contentView.setText(listData.get(position).getCHECKCONTENT());
viewHolder.contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "click "+((TextView)v).getText(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.rb_detection_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listData.get(position).setResultStatus("合格");
}
});
viewHolder.rb_detection_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listData.get(position).setResultStatus("不合格");
}
});
return convertView;
}
}
}
static class ViewHolder
{
public TextView contentView;
public TextView menuView;
public RadioButton rb_detection_1;
public RadioButton rb_detection_2;
public RadioGroup rg_detection_group;
}