All what I need is to save a pair of list that are in the database to use them later in the code, but the lists always appear empty when are called outside the onResponse method. I'm new in both Android Studio and Databases. I used the Volley library, here's an "extract" of the part of the code that's giving me problems with some Prints where the Array is empty or not:
public class Fragmento extends Fragment {
ArrayList<String> codes = new ArrayList<String>();
ArrayList<String> names = new ArrayList<String>();
ArrayList<ArrayList<String>> thing= new ArrayList<ArrayList<String>>();;
RequestQueue requestQueue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
load();
System.out.println(thing); //ARRAY IS EMPTY
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragmento, container, false);
}
private void load(){
JsonArrayRequest jsonArrayRequest= new JsonArrayRequest("the url here",new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
codes.add(jsonObject.getString("codigo"));
names.add(jsonObject.getString("nombre"));
} catch (JSONException e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
thing.add(codes);
thing.add(names);
System.out.println(thing);//ARRAY IS FULL
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
System.out.println(thing);//ARRAY IS EMPTY
requestQueue=Volley.newRequestQueue(getActivity());
requestQueue.add(jsonArrayRequest);
}
}