0

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);
    }
}
The_Martian
  • 3,684
  • 5
  • 33
  • 61
Ultrinik
  • 1
  • 1

1 Answers1

1

Because in load() function you are calling API using JsonArrayRequest in background, so System.out.println(thing) in onCreate() calling before getting response from Server. That's why thing array is empty.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
  • And how could I make the program wait for the response of the server, there's a comment that says that ShareDPreferences is the answer, should I try that? – Ultrinik Feb 26 '20 at 08:02
  • Simple, you can execute you code under `onResponse()`. – Sagar Zala Feb 26 '20 at 08:11