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
Right, I get it. And how would I fill in my Arraylist, if the data I need is in onResponse?
– Vitor Herrmann
The problem is how(where) you are using arraylist as it is filled in. Where you want to use this list?
– ramaral
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
– Vitor Herrmann