我了解ListView回收问题并尝试将我的状态保留在模型中,但似乎无法正常工作。我已经阅读了许多同样问题的主题,但他们的解决方案对我不起作用,或者我做错了什么。 当我在初始状态下使用无线电组向上和向下滚动列表视图时,没有任何反应,这是正确的。但是一旦我设置了任何行中的任何单选按钮,然后上下滚动单选按钮就会随机检查。
RadioAdapter.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;
}
}
Question.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>
我希望你能帮助我找到错误,因为我完全厌倦了寻求解决方案。感谢
答案 0 :(得分:1)
在为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;
}
}
答案 1 :(得分:0)
您可以执行此覆盖getItemId方法并返回您的问题ID,以便您能够使用ID识别每个问题和答案。
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((int) getItemId(position)).setYesChecked(true);
mSource.get((int) getItemId(position)).setNoChecked(false);
break;
case R.id.rbIterationNo:
//Vice versa
mSource.get((int) getItemId(position)).setNoChecked(true);
mSource.get((int) getItemId(position)).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((int) getItemId(position)).isYesChecked() && !mSource.get((int) getItemId(position)).isNoChecked()) {
holder.rbGroup.clearCheck();
} else {
//We are supposing 'YES' is checked
int c = 0;
//Or 'NO' is checked
if (mSource.get((int) getItemId(position)).isNoChecked()) {
c = 1;
}
//Set checked button
((RadioButton) holder.rbGroup.getChildAt(c)).setChecked(true);
}
return row;
}
@Override
public long getItemId(int position) {
return mSource.get(position).getId();
}
}