我有列表视图,每个项目都包含一个文本视图和一个无线电组。Radiogroup 包含两个单选按钮。我的问题是,当我滚动列表视图时,多个联系人被选中(即在列表视图的不可见区域中的相同位置出现的联系人被选中)那么我应该如何克服这个问题,请帮助我?
xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup android:id="@+id/selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/allow"
android:paddingRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/block"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</RelativeLayout>
Adapter code :
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.lv_contact, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.rbg = (RadioGroup) convertView.findViewById(R.id.selection);
holder.rbg.setTag(position);
holder.rbAllow = (RadioButton) convertView.findViewById(R.id.allow);
holder.rbBlock = (RadioButton) convertView.findViewById(R.id.block);
holder.tv_contactNumber = (TextView) convertView.findViewById(R.id.tv_number);
holder.rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Contact contact;
switch (checkedId) {
case R.id.allow:
contact = contactList.get(position);
contact.setAllow(true);
break;
case R.id.block:
contact = contactList.get(position);
contact.setAllow(false);
break;
default:
break;
}
});
} else {
holder = (ViewHolder)convertView.getTag();
convertView.setTag(holder);
}
Contact con = contactList.get(position);
holder.tv_contactNumber.setText(con.getNumber());
if (!con.isAllow() && !con.isBlock()) {
} else {
if (con.isAllow()) {
holder.rbAllow.setChecked(true);
holder.rbBlock.setChecked(false);
} else {
holder.rbAllow.setChecked(false);
holder.rbBlock.setChecked(true);
}
}
return convertView;
}
Contact file :
public class Contact {
private String number;
private boolean Allow;
private boolean Block;
public boolean isAllow() {
return Allow;
}
public void setAllow(boolean isAllow) {
Allow = Allow;
}
public boolean isBlock() {
return Block;
}
public void setBlock(boolean isBlock) {
Block = isBlock;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
public Contact(String number, boolean allow, boolean block) {
name = name;
Allow = allow;
Block = block;
}
}