0

I have an array with 2 positions, each one contains a list of "ids_alunos", the first position has ids_alunos: "1,2,3" and the second:"4,5" but what is happening when I add the ids in my array is this, the first position gets "1,2,3" values and the second: "1,2,3,4,5". why is this happening?

public ArrayList<CadastraEscolas> getFilhos(String mToken) throws Exception {

        String[] resposta = new WebService().get("filhos", mToken);


        if (resposta[0].equals("200")) {

            JSONObject mJSONObject = new JSONObject(resposta[1]);
            JSONArray dados = mJSONObject.getJSONArray("data");



            mArrayList = new ArrayList<CadastraEscolas>();
            mGPSList = new ArrayList<GPSEscolas>();


            for (int i = 0; i < dados.length(); i++) {


                JSONObject item = dados.getJSONObject(i).getJSONObject("escolas");
                JSONArray escolas = item.getJSONArray("data");

                for (int j = 0; j < escolas.length(); j++) {

                    JSONObject jItens = escolas.getJSONObject(j);
                    mCadastraEscolas = new CadastraEscolas();
                    mGPSEscolas = new GPSEscolas();

                    mCadastraEscolas.setId_escola(jItens.optInt("id_escola"));


                    mGPSEscolas.setId_escola(jItens.optInt("id_escola"));



                    JSONObject alunos = jItens.optJSONObject("alunos");

                    JSONArray data = alunos.getJSONArray("data");

                    if (data != null) {

                        ArrayList<Filhos> arrayalunos = new ArrayList<Filhos>();

                        for (int a = 0; a < data.length(); a++) {

                            mFilhos = new Filhos();

                            JSONObject clientes = data.getJSONObject(a);

                            mFilhos.setId_aluno(clientes.optInt("id_aluno"));



                            arrayalunos.add(mFilhos);

                            idsAlunos  += ";" + arrayalunos.get(a).getId_aluno().toString();

                            mGPSEscolas.setIds_alunos(idsAlunos);

                        }


                        mCadastraEscolas.setalunos(arrayalunos);

                    }



                    mArrayList.add(mCadastraEscolas);
                    mGPSList.add(mGPSEscolas);



                }
            }

            return mArrayList;

        } else {
            throw new Exception("[" + resposta[0] + "] ERRO: " + resposta[1]);
        }

    }
2
  • Where is the declaration of idsAlunos ? If this has a global scope, the values from the first iteration will remain for the second iteration and therefore appended to the first value. If you want to avoid this, either give idsAlunos local scope or clear the value at the beginning of each iteration Commented Jun 3, 2015 at 15:09
  • 2
    Use Set instead of ArrayList for avoiding duplicate values. Commented Jun 3, 2015 at 15:10

1 Answer 1

1

You probably want to initialize idsAlunos at the same time as mGPSEscolas, so where you do:

mGPSEscolas = new GPSEscolas();

you could also do:

idsAlunos = "";

otherwise idsAlunos gets appended to with every new mGPSEscolas.

Sign up to request clarification or add additional context in comments.

1 Comment

could you take a look at my other question please? stackoverflow.com/questions/30715299/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.