Charles Galvez
Charles Galvez

Reputation: 1110

Volley responds with delay

I have an database (XAMPP).
I'm able to get the values but I think getting values has delay?
Getting of values is trigger on onClickListener of a RecyclerView.

 class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.listText);
            icon = (ImageView) itemView.findViewById(R.id.listIcon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDBCommet();
                    //context.startActivity(new Intent(context, CategorySelected.class));
                    // Global.selectedPOI = title.getText().toString();
                }
            });
        }
    }


    public void getDBCommet() {
        requestQueue = Volley.newRequestQueue(context);
        RequestHolderPOI jsonObjectRequest = new RequestHolderPOI(Request.Method.POST,
                showUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("poi");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Global.getCommentTitle.add(jsonObject.getString("title"));
                        Global.getCommentPicture.add("drawable://" + R.drawable.juandirection_placeholder);
                        Global.getComment.add(jsonObject.getString("comment"));
                        Global.getCommentDate.add(jsonObject.getString("date"));
                        Global.getCommentRating.add(Integer.parseInt(jsonObject.getString("rating")));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }

        });
        requestQueue.add(jsonObjectRequest);
        Log.e("SIZE", "" + Global.getComment.size());
    }

My database contains only 2 rows.
When I click (once), the size of my array is zero.
And then when I click again the size is two.
Here's sample Log.

11-15 07:19:53.298 2027-2027/? E/SIZE: 0
11-15 07:19:56.746 2027-2027/? E/SIZE: 2
11-15 07:19:57.765 2027-2027/? E/SIZE: 4

The size at first click should be two.

Upvotes: 0

Views: 559

Answers (1)

BNK
BNK

Reputation: 24114

Because Volley is asynchronous so the simple solution for your issue is that you should move Log.e("SIZE", "" + Global.getComment.size()); into onResponse. Hope this helps!

Upvotes: 1

Related Questions