我想制作一个自定义的无线电组列表,应该在每个项目中都有一个文本视图和一个无线电组,每个无线电组都包含单选按钮,我可以这样做但是有一个问题....,收音机按钮重复的次数超过了每个项目的重复次数,我可以选择一个错误的广播组中的多个单选按钮,
这就是我所做的:
PollsQuestionsAdapter
public class PollsQuestionsAdapter extends ArrayAdapter {
Context context;
List<PollQuestion> pollQuestionList;
public PollsQuestionsAdapter(Context context, List<PollQuestion> pollQuestionList) {
super(context, R.layout.polls_questions_list_item, pollQuestionList);
this.context = context;
this.pollQuestionList = pollQuestionList;
}
@Override
public int getCount() {
return pollQuestionList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.polls_questions_list_item, parent, false);
}
RadioGroup rgAnswer;
TextView tvQuestion;
tvQuestion = (TextView) convertView.findViewById(R.id.polls_questions_list_item_question_name);
rgAnswer = (RadioGroup) convertView.findViewById(R.id.polls_questions_list_item_answer);
String questionName;
List<PollAnswer> answerList;
if (Language.lang.equals("EN") || Language.lang.equals("En") || Language.lang.equals("en"))
questionName = pollQuestionList.get(position).getQuestionTextEN();
else
questionName = pollQuestionList.get(position).getQuestionTextAR();
answerList = pollQuestionList.get(position).getAnswersList();
rgAnswer.setTag(pollQuestionList.get(position).getQuestionId()); // to use it when you get the value of question id
for (int i = 0; i < answerList.size(); i++) {
RadioButton radioButton = new RadioButton(context);
if (Language.lang.equals("EN") || Language.lang.equals("En") || Language.lang.equals("en"))
radioButton.setText(answerList.get(i).getAnswerTextEN());
else
radioButton.setText(answerList.get(i).getAnswerTextAR());
radioButton.setId(answerList.get(position).getAnswerId()); // to use it when you get the value of the answer id
rgAnswer.addView(radioButton);
}
tvQuestion.setText(questionName);
return convertView;
}
}
polls_questions_list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E5BAC"
android:orientation="vertical"
android:padding="@dimen/padding_normal">
<TextView
android:id="@+id/polls_questions_list_item_question_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/white"
android:textSize="@dimen/font_large"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/polls_questions_list_item_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white">
</RadioGroup>
</LinearLayout>
以下是我在活动中调用适配器的方式:
List<PollQuestion> pollQuestionList = new ArrayList<>();
ArrayList<PollAnswer> pollAnswerList = new ArrayList<>();
pollAnswerList.add(new PollAnswer(1, "Yes", "نعم"));
pollAnswerList.add(new PollAnswer(2, "No", "لا"));
pollQuestionList.add(new PollQuestion("مواعيد المحاضرات بها تعارض", "Schedule has conflict", 1, pollAnswerList));
pollAnswerList = new ArrayList<>();
pollAnswerList.add(new PollAnswer(3, "High", "مرتفعة"));
pollAnswerList.add(new PollAnswer(4, "Medium", "متوسطة"));
pollAnswerList.add(new PollAnswer(5, "Low", "منخفضة"));
pollQuestionList.add(new PollQuestion("مواعيد المحاضرات موزعة بالتساوى خلال الاسبوع", "The lectures schedule is balanced during week",2, pollAnswerList));
PollsQuestionsAdapter adapter = new PollsQuestionsAdapter(PollsQuestionsActivity.this, pollQuestionList);
listView.setAdapter(adapter);
这就是结果:
第一项应包含: 是和否只是
第二项应包含: 仅高,中,低
并且应该一起选择每个广播组中的单选按钮,只按一个按钮而其他按钮被禁用,这在此处的项目中不会发生。
所以,任何人都可以告诉我,我做错了什么?
提前致谢。