[英]Android Multiple listView item with radiobuttons get selected after scrolling list
我有一個listview,每個項目包含一個textview和一個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;
}
}
這是設計使然,因為列表視圖中的視圖被回收以降低內存消耗。 解決此問題的一種方法是使用提供給列表視圖的數據長度創建一個布爾數組,並在復選框上創建一個偵聽器,此監聽器在單擊時將布爾數組中的位置設置為true或false。 然后,在聲明偵聽器的適配器中,只需使用存儲在數組索引中的值初始化每個復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.