1

I have this method which receives data from a db:

public ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();

public ArrayList<ArrayList<String>> scaricaDatiUtente(){

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, DatiNet.MostraDati, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response.toString());

            try {
                JSONArray utenti = response.getJSONArray("utenti");

                for (int i = 0; i < utenti.length(); i++) {
                    JSONObject student = utenti.getJSONObject(i);
                    ArrayList<String> studentdata = new ArrayList<String>();
                    studentdata.add(student.getString("nome"));
                    studentdata.add(student.getString("cognome"));
                    studentdata.add(student.getString("numero"));
                    studentdata.add(student.getString("email"));

                    result.add(studentdata);

                }

            } catch (JSONException e) {
                e.printStackTrace();

            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.append(error.getMessage());

        }
    });
    requestQueue.add(jsonObjectRequest);
    return result;
}

The problem is that the result is returned before data is downloaded. How can make it wait for data and then return?

abarisone
  • 3,707
  • 11
  • 35
  • 54
Alessandro Zago
  • 793
  • 3
  • 12
  • 33

0 Answers0