我知道 ListView 回收问题并尝试将我的状态保留在模型中,但它似乎不起作用。我读过很多有同样问题的主题,但他们的解决方案对我不起作用,或者我做错了什么。 当我在初始状态下上下滚动 ListView 时,没有任何反应,没错。但是一旦我设置选中任何行中的任何单选按钮,然后上下滚动它,单选按钮就会随机选中。
radio 适配器.java:
public class RadioAdapter extends ArrayAdapter<Question> {
List<Question> mSource;
static class ViewHolder {
TextView category = null;
TextView question = null;
RadioGroup rbGroup = null;
ViewHolder(View row) {
this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
this.question = (TextView) row.findViewById(R.id.tvQuestion);
this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
}
}
private LayoutInflater mInflater;
public RadioAdapter(Context context, List<Question> mSource) {
super(context, R.layout.item_question, mSource);
mInflater = LayoutInflater.from(context);
this.mSource = mSource;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.item_question, null);
holder = new ViewHolder(row);
holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
//Get position of current group
Integer pos = (Integer) group.getTag();
switch (checkedId) {
case R.id.rbIterationYes:
//Check 'YES' and uncheck 'NO'
mSource.get(pos).setYesChecked(true);
mSource.get(pos).setNoChecked(false);
break;
case R.id.rbIterationNo:
//Vice versa
mSource.get(pos).setNoChecked(true);
mSource.get(pos).setYesChecked(false);
break;
}
}
});
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.rbGroup.setTag(new Integer(position));
holder.category.setText(mSource.get(position).getCategory());
holder.question.setText(mSource.get(position).getDescription());
//If no one of buttons isn't checked
if (!mSource.get(position).isYesChecked() && !mSource.get(position).isNoChecked()) {
holder.rbGroup.clearCheck();
}else {
//We are supposing 'YES' is checked
int c = 0;
//Or 'NO' is checked
if (mSource.get(position).isNoChecked()){
c = 1;
}
//Set checked button
((RadioButton)holder.rbGroup.getChildAt(c)).setChecked(true);
}
return row;
}
问题.java:
public class Question {
private Integer id;
private String category;
private String description;
private boolean yesChecked;
private boolean noChecked;
/* Getters and setters */
}
item_question.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/tvQuestionCategory"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/tvQuestion"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rgIteration">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/rbIterationYes"
android:text="@string/rbIterationYes"
android:textSize="16dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/rbIterationNo"
android:text="@string/rbIterationNo"
android:textSize="16dp" />
</RadioGroup>
</LinearLayout>
我希望你能帮我找出错误,因为我已经厌倦了寻求解决方案。谢谢
最佳答案
在为您的 RadioGroup
设置选中或未选中之前,您应该禁用监听器,如下所示。希望这可以帮助。
将您的适配器更改为此
public class RadioAdapter extends ArrayAdapter<Question> {
List<Question> mSource;
RadioGroup.OnCheckedChangeListener listener;
static class ViewHolder {
TextView category = null;
TextView question = null;
RadioGroup rbGroup = null;
ViewHolder(View row) {
this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
this.question = (TextView) row.findViewById(R.id.tvQuestion);
this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
}
}
private LayoutInflater mInflater;
public RadioAdapter(Context context, List<Question> mSource) {
super(context, R.layout.item_question, mSource);
mInflater = LayoutInflater.from(context);
this.mSource = mSource;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.item_question, null);
holder = new ViewHolder(row);
listener = new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
//Get position of current group
Integer pos = (Integer) group.getTag();
switch (checkedId) {
case R.id.rbIterationYes:
//Check 'YES' and uncheck 'NO'
mSource.get(pos).setYesChecked(true);
mSource.get(pos).setNoChecked(false);
break;
case R.id.rbIterationNo:
//Vice versa
mSource.get(pos).setNoChecked(true);
mSource.get(pos).setYesChecked(false);
break;
}
}
};
holder.rbGroup.setOnCheckedChangeListener(listener);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.rbGroup.setTag(new Integer(position));
holder.category.setText(mSource.get(position).getCategory());
holder.question.setText(mSource.get(position).getDescription());
//If no one of buttons isn't checked
holder.rbGroup.setOnCheckedChangeListener(null);
if (!mSource.get(position).isYesChecked() && !mSource.get(position).isNoChecked()) {
holder.rbGroup.clearCheck();
}else {
//We are supposing 'YES' is checked
int c = 0;
//Or 'NO' is checked
if (mSource.get(position).isNoChecked()){
c = 1;
}
//Set checked button
((RadioButton)holder.rbGroup.getChildAt(c)).setChecked(true);
}
holder.rbGroup.setOnCheckedChangeListener(listener);
return row;
}
}
关于android - RadioGroup 没有在 ListView 中保持正确的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35845816/