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,