自动选择列表视图选项按钮

时间:2014-06-10 10:40:54

标签: android android-listview radio-button android-custom-view

我有自定义列表视图在这里我使用单选按钮,如果我选择第一个listitem,自动列表视图最后一项也被选中如何避免这个问题?

这是我的代码:

适配器类中的

public class QuestionsAdapter extends ArrayAdapter<Question> {

private LayoutInflater mInflater;

public QuestionsAdapter(Context context, List<Question> data) {
    super(context, R.layout.questions_lits_item, data);
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Question question = getItem(position);
    View view;
    if (convertView == null) {
        view = mInflater.inflate(R.layout.questions_lits_item, null, true);
    } else {
        view = convertView;
    }
    RadioButton option1, option2, option3, option4;
    option1 = (RadioButton) view.findViewById(R.id.option1);
    option2 = (RadioButton) view.findViewById(R.id.option2);
    option3 = (RadioButton) view.findViewById(R.id.option3);
    option4 = (RadioButton) view.findViewById(R.id.option4);

    ((TextView) view.findViewById(R.id.question)).setText((position + 1)+ ". " + question.getQuestion());
    option1.setText(question.getOption1());
    option2.setText(question.getOption2());
    if (!question.getOption3().isEmpty()) {
        option3.setText(question.getOption3());
    } else {
        option3.setVisibility(View.GONE);
    }
    if (!question.getOption4().isEmpty()) {
        option4.setText(question.getOption4());
    } else {
        option4.setVisibility(View.GONE);
    }

    option1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            question.setSelectedAnswer(question.getOption1());
        }
    });
    option2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            question.setSelectedAnswer(question.getOption2());
        }
    });
    option3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            question.setSelectedAnswer(question.getOption3());
        }
    });
    option4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            question.setSelectedAnswer(question.getOption4());
        }
    });

    return view;
}
  

列表:问题是一个带有getter和setter方法的类

package com.griffin.android.schoolapp.adapter;

公共课问题{

private String id;
private String question;
private String option1;
private String option2;
private String option3;
private String option4;
private String selectedAnswer = "null";

public Question(String id, String question, String option1, String option2,
        String option3, String option4) {
    super();
    this.id = id;
    this.question = question;
    this.option1 = option1;
    this.option2 = option2;
    this.option3 = option3;
    this.option4 = option4;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public String getOption1() {
    return option1;
}

public void setOption1(String option1) {
    this.option1 = option1;
}

public String getOption2() {
    return option2;
}

public void setOption2(String option2) {
    this.option2 = option2;
}

public String getOption3() {
    return option3;
}

public void setOption3(String option3) {
    this.option3 = option3;
}

public String getOption4() {
    return option4;
}

public void setOption4(String option4) {
    this.option4 = option4;
}

@Override
public String toString() {
    return question;
}

public String getSelectedAnswer() {
    return selectedAnswer;
}

public void setSelectedAnswer(String selectedAnswer) {
    this.selectedAnswer = selectedAnswer;
}}

1 个答案:

答案 0 :(得分:0)

您必须明确setCheckedId()应该检查的单选按钮ID,就像您设置的文本和可见性一样,因为项目视图将被回收,每次调用getView时它将要么膨胀新视图/回收现有视图,在这种情况下,它可以延续另一个项目的选定状态。

还尝试使用ViewHolder模式以获得更好的UI性能。