Пользовательский вид списка Android с проблемой радиогруппы

Я хочу создать пользовательский список радиогрупп, в котором должно быть текстовое представление и радиогруппа в каждом элементе, и каждая радиогруппа содержит радиокнопки, я мог бы сделать это, но есть проблема...., радиокнопки повторяются больше, чем должно быть в каждом элементе, и я могу выбрать несколько радиокнопок в группе радиостанций, что неправильно,

вот что я сделал:

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);

и вот результат:

result1

result2

первый элемент должен содержать: да и нет только

второй элемент должен содержать: только High, Medium и Low

и радиокнопки в каждой радиогруппе должны быть выбраны вместе, только одна кнопка нажата, а остальные отключены, чего не происходит в пунктах здесь.

Итак, кто-нибудь может сказать мне, что я сделал не так?

Заранее спасибо.

0 ответов

Другие вопросы по тегам