0

Newbie. I'm coding a quiz app full code on Github that loads an arrayList with four arguments:

  1. question
  2. image (from drawables)
  3. key answer
  4. possible answers presented in a radioGroup (sub-arrayList)

from the strings.xml as below

    ...
    <string name="questionOne">Who is the "Modern Love" rock star singer?</string>
    <string name="answerOne">David Bowie</string>

    <string-array name="celebrityOne">
        <item>Jaimie Hendrix</item>
        <item>David Bowie</item>
        <item>Jim Morrison</item>
        <item>Elvis Presley</item>
    </string-array>
    ...

Below is how the arguments are loaded in MainActivity (The third argument is a sub-arraylist)

    ArrayList<Object> arrayList = new ArrayList<>();
    loaddata()

    ...

    public void loadData() {
        arrayList.add(new Quiz(getResources().getString(R.string.questionOne), 
                getResources().getDrawable(R.drawable.celebrity_one_image, null), 
                new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.celebrityOne))),
                getResources().getString(R.string.answerOne)));   

        arrayList.add(new Quiz(getResources().getString(R.string.questionTwo), 
                getResources().getDrawable(R.drawable.celebrity_two_image, null), 
                new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.celebrityTwo))),
                getResources().getString(R.string.answerTwo)));

        ...  

    }

The issue is after N iterations, the sub-arrayList starts repeating itself (See image below).

Also I think maybe the source of the problem is in the Adapter, where for each string in sub-array is assigned to a radioButton;


      void createRadioButtons(String[] arrayAnswer) {
            if (mRadioGroup.getChildAt(0) != null)
                return;
            for (int i = 0; i < arrayAnswer.length; i++) {
                mRadioGroup.addView(createRadioButtonAnswerAndSetOnClickListener(arrayAnswer[i]));
            }
        }

        RadioButton createRadioButtonAnswerAndSetOnClickListener(String string) {
            RadioButton radioButton = new RadioButton(mContext);
            radioButton.setText(string);
            radioButton.setOnClickListener(this);
            return radioButton;
        }

First Image Second image

My situation might be similar to this but I have no static fields and arrayList is initialized as new so no need to clear().

axelmukwena
  • 779
  • 7
  • 24

2 Answers2

2

From Documentation:

The RecyclerView creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen.

This means RecyclerView reuses already created view holders when you are scrolling it(that is why your data repeats), and you must repopulate views with new data. So, instead of returning from createRadioButtons method, when mRadioGroup.getChildAt(0) != null, you must change RadioButtons texts to your new data from arrayAnswer.

Garnik
  • 145
  • 6
1

in your adapter just change this:

  if (mRadioGroup.getChildAt(0) != null)
            return;

To this:

if (mRadioGroup.getChildAt(0) != null)
            mRadioGroup.removeAllViews();

At some moment your adapter, began to reuse view holders which were created at the top of the recyclerView, but it was already filled with data, so when you call return, you just leave your old data, while you need to delete it and then add new data...