I add items to Arraylist but when I use it it is empty

Asked

Viewed 247 times

1

I cannot 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 the 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 into the Array, but when it accesses the Array after this function, it is with size() == 0, i.e., empty. Where is my mistake?

From now on, thank you

1 answer

2


Note that it is not the method setArrayMedico() who fills in the list but the method onResponse(), class Jsonarrayrequest.

Put another way: when the method setArrayMedico() is called it returns immediately before the list is filled in.

If the code you’re using is something like this:

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

isEmpty will be true.

A possible solution will be:

  • Do not declare Arraylist as an Activity attribute, this will ensure that it is not used unfilled.

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

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

    @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);
    }
    
  • Right, I get it. And how would I fill in my Arraylist, if the data I need is in onResponse?

  • The problem is how(where) you are using arraylist as it is filled in. Where you want to use this list?

  • Yes, it is filled, but when I use it is empty. The ideal would be to call the setArrayMedico() to popular my array, and on the bottom line, throw the dice to a Listview

Browser other questions tagged

You are not signed in. Login or sign up in order to post.