Android ListView with ArrayAdapter replacing list item components

五迷三道 提交于 2019-12-23 03:19:19

问题


Please check the last update down below as I found the issue but don't know how to fix it

[Update: project download link] which is very tiny it's just a sample and added Questions class

I'm creating a ListView with ArrayAdapter for a quiz. every question has 3 answers as radio buttons. The problem is when I scroll down or up again it replaces the answers of the first question with answers of another question which was hidden by the scroll and replaces the hidden question answers with the first question answers.

So let's say I have question one with 3 answers: 1, 2 and 3 and have question 5 which is the first hidden question with three answers 10,11 and 12. When I scroll down to view question 5 I find that its answers are 1,2 and 3 then when I scroll up I find that question one has the answers of question 5 which are 10,11 and 12. Why is this happening and how to fix it? Here's the adapter

public class QuestionsAdapter extends ArrayAdapter<Questions> {

public QuestionsAdapter(Activity context, ArrayList<Questions> questions) {
    super(context, 0, questions);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }
        //styling odd and even items
        if (position % 2 == 1) {
            listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.odd));
        } else {
            listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.even));
        }

        //used ViewHolder to prevent triggering position null issue
        // final because it'll be used inside onClick
        final ViewHolder viewHolder = new ViewHolder();

        try {
            final Questions currentQuestion = getItem(position);

            viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
            viewHolder.questionTextView.setText(currentQuestion.getmQuestionTitle());

            viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);

            viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);

           //MainActivity.answerIdis set to 0 and use it to set different id for each radio to find it inside onClick
           viewHolder.firstAnswer.setText(currentQuestion.getQuestionAnswer1());
            viewHolder.firstAnswer.setId(MainActivity.answerId);
          //increading MainActivity.answerId by 1
            MainActivity.answerId +=1;

            viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
            viewHolder.secondAnswer.setText(currentQuestion.getQuestionAnswer2());
            viewHolder.secondAnswer.setId(MainActivity.answerId);
            MainActivity.answerId +=1;

            viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
            viewHolder.thirdAnswer.setText(currentQuestion.getQuestionAnswer3());
            viewHolder.thirdAnswer.setId(MainActivity.answerId);
            MainActivity.answerId +=1;

            final TextView resultTextView = (TextView) listItemView.findViewById(R.id.result_text);


        } catch (Exception e) {
            e.printStackTrace();
        }

    return listItemView;
}

  private class ViewHolder {
    TextView questionTextView;
    RadioGroup radioGroup;
    RadioButton firstAnswer, secondAnswer,thirdAnswer;
  }
}

Questions class:

public class Questions {

  private String mQuestionTitle,mQuestionAnswer1, mQuestionAnswer2, mQuestionAnswer3, mCorrect ;

  public Questions(String questionTitle, String questionAnswer1, String questionAnswer2, String questionAnswer3,
                 String correct){
    mQuestionTitle = questionTitle;
    mQuestionAnswer1 = questionAnswer1;
    mQuestionAnswer2 = questionAnswer2;
    mQuestionAnswer3 = questionAnswer3;
    mCorrect = correct;
  }

  public String getQuestionTitle(){
    return mQuestionTitle;
  }

  public String getQuestionAnswer1(){
    return mQuestionAnswer1;
  }

  public String getQuestionAnswer2(){
    return mQuestionAnswer2;
  }

  public String getQuestionAnswer3(){
    return mQuestionAnswer3;
  }

  public String getCorrect(){
    return mCorrect;
  }

}

[Update] trying to debug and display the question again along with the first radio button I find that the question title is different from the question i display along with the radio button

String question = currentQuestion.getQuestionTitle();
String option_1 = currentQuestion.getQuestionAnswer1();
String option_2 = currentQuestion.getQuestionAnswer2();
String option_3 = currentQuestion.getQuestionAnswer3();
viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
viewHolder.questionTextView.setText(question);
viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);
viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
viewHolder.firstAnswer.setText(option_1 + ":" + question);
viewHolder.firstAnswer.setId(MainActivity.answerId);
MainActivity.answerId +=1;

The real issue here when I set the id of the answer it returns to 0 and starts increasing it by 1 and when scolling down again it returns to 0 again. How can I save the last integer value and keep increasing it to set different ids to radio buttons?

Screenshots

来源:https://stackoverflow.com/questions/55281600/android-listview-with-arrayadapter-replacing-list-item-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!