I add items to the ArrayList but when I use it it is empty

1

I can not add items to my ArrayList

Main class where the array is instantiated

public class act_principal extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{

    private List<Medico> medicos = new ArrayList<Medico>();

Method where items should be added to Array

public void setArrayMedico()
    {
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url_getMedicos, (String)null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                int codigo = 0;
                String nome="";

                for(int i=0; i<response.length(); i++)
                {
                    try {
                        JSONObject jsonObject = response.getJSONObject(i);

                        codigo = jsonObject.getInt("Codigo");
                        nome = jsonObject.getString("Nome");

                        medicos.add(new Medico(codigo, nome));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        MySingleton.getInstance(this).addToRequest(jsonArrayRequest);
    }

When debugging, it normally inserts the items in the Array, but when you access the Array after this function, it is with size () == 0, that is, empty. Where is my mistake?

Thanks in advance,

    
asked by anonymous 01.09.2016 / 18:15

1 answer

2

Note that it is not the setArrayMedico() method that fills the list, but the onResponse() method, of the JsonArrayRequest class .

Put another way: When the setArrayMedico() method is called it returns immediately, before the list is populated.

If the code you are using is something like this:

setArrayMedico();
boolean isEmpty = medicos.isEmpty();

isEmpty will true .

One possible solution will be:

  • Do not declare the ArrayList as an Activity attribute, this will ensure that it is not used without being populated.

  • Declare a method that will be called when the list is populated:

    private void onMedicosIsReady(List<Medico> medicos){
    
        //Utilize aqui a lista
    }
    
  • In method onResponse() call this method after the list is populated:

    @Override
    public void onResponse(JSONArray response) {
        int codigo = 0;
        String nome="";
    
        //Declaração do ArrayList
        List<Medico> medicos = new ArrayList<Medico>();
    
        for(int i=0; i<response.length(); i++)
        {
            try {
                JSONObject jsonObject = response.getJSONObject(i);
    
                codigo = jsonObject.getInt("Codigo");
                nome = jsonObject.getString("Nome");
    
                medicos.add(new Medico(codigo, nome));
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        //Chama o método quando estiver preenchido
        onMedicosIsReady(medicos);
    }
    
01.09.2016 / 18:27