android listview里面Radiogroup监听事件混乱

public class AdapterTest extends ArrayAdapter {

    private int resourceId;
    List<Test> listTest = new ArrayList<Test>();
    private ViewHolder viewHolder;

    public AdapterTest(Context context, int resource, 

List objects) {
super(context, resource, objects);
resourceId = resource;
listTest = objects;
}

    @Override
    public View getView(final int position, View 

convertView,
ViewGroup parent) {

        viewHolder = new ViewHolder();
        View view = LayoutInflater.from(getContext

()).inflate(resourceId,
null);

        viewHolder.tvTitle = (TextView) view
                .findViewById

(R.id.tv_test_title);
viewHolder.rdoG = (RadioGroup)

view.findViewById(R.id.rdog_test);
viewHolder.radA = (RadioButton)

view.findViewById(R.id.rdo_test_a);
viewHolder.radB = (RadioButton)

view.findViewById(R.id.rdo_test_b);
viewHolder.radC = (RadioButton)

view.findViewById(R.id.rdo_test_c);
viewHolder.radD = (RadioButton)

view.findViewById(R.id.rdo_test_d);
viewHolder.tvAnswer = (TextView) view
.findViewById

(R.id.tv_test_answer);

        viewHolder.tvTitle.setText(String.valueOf

(position + 1) + "、"
+ listTest.get

(position).getTitle());
viewHolder.radA.setText("A、"
+ listTest.get

(position).getQuestionA());
viewHolder.radB.setText("B、"
+ listTest.get

(position).getQuestionB());
viewHolder.radC.setText("C、"
+ listTest.get

(position).getQuestionC());
viewHolder.radD.setText("D、"
+ listTest.get

(position).getQuestionD());
viewHolder.tvAnswer.setText("答案:"
+ listTest.get

(position).getAnswer());

        // viewHolder.rdoG.setTag(position);
        if (listTest.get(position).getChecked

().equals("A")
|| listTest.get

(position).getChecked().equals("B")
|| listTest.get

(position).getChecked().equals("C")
|| listTest.get

(position).getChecked().equals("D")) {

            if (listTest.get(position).getChecked

().equals("A")) {
viewHolder.radA.setChecked

(true);
if (listTest.get

(position).getAnswer()
.equals

(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);
}
if (listTest.get(position).getChecked

().equals("B")) {
viewHolder.radB.setChecked

(true);
if (listTest.get

(position).getAnswer()
.equals

(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);
}
if (listTest.get(position).getChecked

().equals("C")) {
viewHolder.radC.setChecked

(true);
if (listTest.get

(position).getAnswer()
.equals

(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);
}
if (listTest.get(position).getChecked

().equals("D")) {
viewHolder.radD.setChecked

(true);
if (listTest.get

(position).getAnswer()
.equals

(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);
}
viewHolder.radA.setEnabled(false);
viewHolder.radB.setEnabled(false);
viewHolder.radC.setEnabled(false);
viewHolder.radD.setEnabled(false);
} else {
viewHolder.rdoG.clearCheck();
viewHolder.tvAnswer.setVisibility

(View.GONE);
}
viewHolder.rdoG.setOnCheckedChangeListener(new

OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged

(RadioGroup arg0, int arg1) {
if(viewHolder.rdoG == arg0){
switch (arg1) {
case R.id.rdo_test_a:
listTest.get

(position).setChecked("A");

                        if 

(listTest.get(position).getAnswer()

.equals(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);

viewHolder.radA.setEnabled(false);

viewHolder.radB.setEnabled(false);

viewHolder.radC.setEnabled(false);

viewHolder.radD.setEnabled(false);
break;

                    case R.id.rdo_test_b:
                        listTest.get

(position).setChecked("B");

                        if 

(listTest.get(position).getAnswer()

.equals(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);

viewHolder.radA.setEnabled(false);

viewHolder.radB.setEnabled(false);

viewHolder.radC.setEnabled(false);

viewHolder.radD.setEnabled(false);
break;

                    case R.id.rdo_test_c:
                        listTest.get

(position).setChecked("C");

                        if 

(listTest.get(position).getAnswer()

.equals(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);

viewHolder.radA.setEnabled(false);

viewHolder.radB.setEnabled(false);

viewHolder.radC.setEnabled(false);

viewHolder.radD.setEnabled(false);
break;

                    case R.id.rdo_test_d:
                        listTest.get

(position).setChecked("D");

                        if 

(listTest.get(position).getAnswer()

.equals(listTest.get(position).getChecked()))

viewHolder.tvAnswer.setTextColor(0xff008000);
else

viewHolder.tvAnswer.setTextColor(0xffdc143c);

viewHolder.tvAnswer.setVisibility(View.VISIBLE);

viewHolder.radA.setEnabled(false);

viewHolder.radB.setEnabled(false);

viewHolder.radC.setEnabled(false);

viewHolder.radD.setEnabled(false);
break;

                    default:
                        break;
                    }
                }

            }
        });

        return view;
    }

    class ViewHolder {
        TextView tvTitle;
        RadioGroup rdoG;
        RadioButton radA;
        RadioButton radB;
        RadioButton radC;
        RadioButton radD;
        TextView tvAnswer;

    }
}

}

6个回答

你这问题就是viewholder 的复用问题。
你需要为每个组件设置点击事件。在设置点击事件的时候需要注意:需要保存每一个viewholder。
贴上一段代码:
可以参考一下

 private class AttenOnclickListener implements View.OnClickListener {
        private int position;
        private ViewHolderHouse viewHoder;

        public AttenOnclickListener(int position, ViewHolderHouse viewHoder) {
            this.position = position;
            this.viewHoder = viewHoder;
        }

        @Override
        public void onClick(View v) {
            if (!checkIsLogin()) {
                T.showShort(context, "请先登录!");
            } else {
                startProgressDialog("");
                if ("1".equals(list.get(position).getAttentFlag())) {// 取消关注
                    attentionOrNot(viewHoder, position, "0");
                } else {// 关注
                    attentionOrNot(viewHoder, position, "1");
                }
            }
        }
    }

设置点击事件:

 hHolder.zanLl.setOnClickListener(new ZanOnclickListener(position, hHolder));

这个是一个选择题,选中后答案显示,并且禁用radiobutton图片说明

看着代码乱,先规整了一下代码。没调试,逻辑本身看着没有太大问题,但像ViewHolder这里你是没有起任何优化作用。有了反而成了累赘了。可以看一下ViewHolder怎么用的,这已经满大街都是了,注意一下Item的复用问题就可以了。
有问题就直接把问题问出来。


public class AdapterTest extends ArrayAdapter {
    private int resourceId;
    List<Test> listTest = new ArrayList<Test>();
    private ViewHolder viewHolder;

    public AdapterTest(Context context, int resource, 
List objects) {
super(context, resource, objects);
resourceId = resource;
listTest = objects;
}  
    private void setAnswer(TextView tv,boolean isRight){
            if (isRight)
                tv.setTextColor(0xff008000);
            else
                tv.setTextColor(0xffdc143c);
                tv.setVisibility(View.VISIBLE);
            }
    }

    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
        viewHolder = new ViewHolder();
        View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
        viewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_test_title);
        viewHolder.rdoG = (RadioGroup)view.findViewById(R.id.rdog_test);
        viewHolder.radA = (RadioButton)view.findViewById(R.id.rdo_test_a);
        viewHolder.radB = (RadioButton)view.findViewById(R.id.rdo_test_b);
        viewHolder.radC = (RadioButton)view.findViewById(R.id.rdo_test_c);
        viewHolder.radD = (RadioButton)view.findViewById(R.id.rdo_test_d);
        viewHolder.tvAnswer = (TextView) view.findViewById(R.id.tv_test_answer);
        viewHolder.tvTitle.setText(String.valueOf(position + 1) + "、"+ listTest.get(position).getTitle());
        viewHolder.radA.setText("A、"+ listTest.get(position).getQuestionA());
        viewHolder.radB.setText("B、"+ listTest.get(position).getQuestionB());
        viewHolder.radC.setText("C、"+ listTest.get(position).getQuestionC());
        viewHolder.radD.setText("D、"+ listTest.get(position).getQuestionD());
        viewHolder.tvAnswer.setText("答案:"+ listTest.get(position).getAnswer());


        viewHolder.rdoG.clearCheck();
        if(listTest.get(position).getChecked()!=null&&listTest.get(position).getChecked().trim().length()>0){
            viewHolder.radA.setEnabled(false);
            viewHolder.radB.setEnabled(false);
            viewHolder.radC.setEnabled(false);
            viewHolder.radD.setEnabled(false);

            this.setAnswer(viewHolder.tvAnswer, listTest.get(position).isRight());
            viewHolder.radA.setChecked(listTest.get(position).getChecked("A".equals(listTest.get(position).getChecked())));
            viewHolder.radB.setChecked(listTest.get(position).getChecked("B".equals(listTest.get(position).getChecked())));
            viewHolder.radC.setChecked(listTest.get(position).getChecked("C".equals(listTest.get(position).getChecked())));
            viewHolder.radD.setChecked(listTest.get(position).getChecked("D".equals(listTest.get(position).getChecked())));
        }else{
            viewHolder.tvAnswer.setVisibility(View.GONE);
        }
        // viewHolder.rdoG.setTag(position);

viewHolder.rdoG.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
if(viewHolder.rdoG == arg0){
switch (arg1) {
case R.id.rdo_test_a:
    listTest.get(position).setChecked("A");
    setAnswer(viewHolder.tvAnswer,listTest.get(position).isRight());                    
break;
                    case R.id.rdo_test_b:
                        listTest.get(position).setChecked("B");
break;
                    case R.id.rdo_test_c:
                        listTest.get(position).setChecked("C");
break;
                    case R.id.rdo_test_d:
                        listTest.get(position).setChecked("D");
break;
                    default:
                        break;
                    }

viewHolder.tvAnswer.setVisibility(View.VISIBLE);
viewHolder.radA.setEnabled(false);
viewHolder.radB.setEnabled(false);
viewHolder.radC.setEnabled(false);
viewHolder.radD.setEnabled(false);
                }

            }
        });

        return view;
    }

    class ViewHolder {
        TextView tvTitle;
        RadioGroup rdoG;
        RadioButton radA;
        RadioButton radB;
        RadioButton radC;
        RadioButton radD;
        TextView tvAnswer;

    }
}
}



public class Test {
  private String answer = "";
  private String checked = "";
  private String questionA = "";
  private String questionB = "";
  private String questionC = "";
  private String questionD = "";
  private String title = "";
public String getAnswer() {
    return answer;
}
public void setAnswer(String answer) {
    this.answer = answer;
}
public String getChecked() {
    return checked;
}
public void setChecked(String checked) {
    this.checked = checked;
}
public String getQuestionA() {
    return questionA;
}
public void setQuestionA(String questionA) {
    this.questionA = questionA;
}
public String getQuestionB() {
    return questionB;
}
public void setQuestionB(String questionB) {
    this.questionB = questionB;
}
public String getQuestionC() {
    return questionC;
}
public void setQuestionC(String questionC) {
    this.questionC = questionC;
}
public String getQuestionD() {
    return questionD;
}
public void setQuestionD(String questionD) {
    this.questionD = questionD;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public boolean isRight(){
    return this.checked!=null&&this.checked.trim().length()>0&&this.checked.equals(this.answer);
}
}
woashizhangsi
泉_哥 赞同
4 年多之前 回复

我点击radiogroup的时候另一个radiogroup会有反应

就是radiogroup的监听事件有问题

保存viewHolder后需要做什么事件处理,能写一些具体的代码吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!