1

I want to see my array list in my logcat but when I run my app in logcat it shows that size of my array list is zero and there is no elements in it

this is my activity

   public class Tmp2 extends AppCompatActivity {
        public RequestQueue queue;
        static ArrayList<Question> array_list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tmp2);
            array_list = new ArrayList<>();
            queue= AppController.getInstance(this).getRequestQueue();
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
                    "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json",
                    (JSONArray) null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            for(int i=0;i<response.length();i++) {
                                try {
                                    Question question = new Question();
                                    question.setAnswer(response.getJSONArray(i).get(0).toString());
                                    question.setAnswertrue((Boolean) response.getJSONArray(i).get(1));
                                    array_list.add(question);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }Log.d("wakwash","array "+array_list.size());
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            Log.d("json","array "+array_list.size());
            queue.add(jsonArrayRequest);
            Log.d("json","array "+array_list);
        }
    }

Logcat:

2020-10-25 00:32:43.548 18576-18576/com.example.practice D/json: array 0
2020-10-25 00:32:43.548 18576-18576/com.example.practice D/json: array []

its showing that size of my array is zero and there is no element it but when i use this log in onResonse it gives sizeand elements both. How can I access my array list out of the response

1
  • Please don't abuse bold. It makes your post harder to read
    – Zoe is on strike
    Oct 24, 2020 at 19:36

1 Answer 1

1

This is because you are logging your array size at wrong place even before you're getting the response, you are making a call which is asynchronous but the log that you've written is executed in synchronous manner that will always be empty. Log your data when your response is received i.e in onResponse callback.

public class Tmp2 extends AppCompatActivity {
        public RequestQueue queue;
        static ArrayList<Question> array_list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tmp2);
            array_list = new ArrayList<>();
            queue= AppController.getInstance(this).getRequestQueue();
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
                    "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json",
                    (JSONArray) null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            for(int i=0;i<response.length();i++) {
                                try {
                                    Question question = new Question();
                                    question.setAnswer(response.getJSONArray(i).get(0).toString());
                                    question.setAnswertrue((Boolean) response.getJSONArray(i).get(1));
                                    array_list.add(question);
                                    
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }Log.d("wakwash","array "+array_list.size());
                             // Log it over here
                             Log.d("json","array "+array_list.size());                     
                             Log.d("json","array "+array_list);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            //Log removed from here
            queue.add(jsonArrayRequest);
        }
    }

You have to do your work after the response is received rather than doing it outside of the response scope, You can also make a callback for your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.