There is a class itemList

 public class itemList implements Serializable { public String nameList; public String Tag; public ArrayList<itemQuestion> itemQuestions; public itemList(String nameList, String Tag,ArrayList<itemQuestion> itemQuestions) { this.nameList = nameList; this.Tag = Tag; this.itemQuestions = itemQuestions; } } 

It has an array with the itemQuestion class itemQuestion

 public class itemQuestion implements Serializable { public String question; public ArrayList<itemAnswer> answer; public int quantityTrue; public int quantityFalse; public itemQuestion(String question,ArrayList<itemAnswer> answer, int quantityTrue, int quantityFalse) { this.question = question; this.answer = answer; this.quantityTrue = quantityTrue; this.quantityFalse = quantityFalse; } } 

And in the itemQuestion class itemQuestion there is another array with itemAnswer

 public class itemAnswer implements Serializable { public String Answer; public boolean trueAnswer; public itemAnswer (String Answer, boolean trueAnswer) { this.Answer = Answer; this.trueAnswer = trueAnswer; } } 

So, basically, there is a variable with the itemList class

 public itemList itemListS = new itemList("","arif",new ArrayList<itemQuestion>()); 

I add a new item to the itemListS element of its itemQuestions array itemQuestions

 public ArrayList<itemAnswer> answer = new ArrayList<itemAnswer>(); //... Здесь заполняю массив answer и т.д. ... // itemListS.itemQuestions.add(new itemQuestion(ArifEditText.getText().toString().toLowerCase(), answer, 0, 0)); answer.clear(); 

And the whole problem is that when, for example, I add a new element to the itemQuestions array, then the elements that already exist in the array take on the value of the answer of the element that was added last.

(Schematic examples will go on, so it’s not necessary to write that I fill the array in the wrong way, etc.)

For example: Suppose that this is an array before filling

 // ... Первый элемент ... // question = "150" ArrayList<itemAnswer> answer = {1 false, 0 false, 6 false, 5 false}; quantityTrue = 0; quantityFalse = 0; 

And here I add the second element

 // ... Первый элемент ... // question = "150" ArrayList<itemAnswer> answer = {2 false, 5 false, 8 false, 7 false}; quantityTrue = 0; quantityFalse = 0; // ... Второй элемент ... // question = "200" ArrayList<itemAnswer> answer = {2 false, 5 false, 8 false, 7 false}; quantityTrue = 0; quantityFalse = 0; 

And as you can see, the array of the first element's answer takes the value of the second element's answer , I think explained is available.

  • try setting final in all signatures - Senior Pomidor
  • in my opinion, you have in vain described "schematically". You do not recreate every time answer , but do answer.clear() and fill it with new elements? - zRrr
  • @zRrr This is yes, what I described "schematically" is that I described the structure which is at the time in itemListS.ItemQuestions - Andrej

1 answer 1

How strange it is all, but after a day of problems, it was all decided something like this

 final ArrayList<itemAnswer> answers = new ArrayList<itemAnswer>(); for (int i=0; i<answer.size(); i++) { answers.add(new itemAnswer(answer.get(i).Answer, answer.get(i).trueAnswer)); } final itemQuestion itemQuestionAdd = new itemQuestion(ArifEditText.getText().toString().toLowerCase(), answers, 0,0); itemListS.itemQuestions.add(itemQuestionAdd); itemListS.nameList = nameList.getText().toString(); 

Even if I just substitute final answers = answer it does not work