0

My volley request is working however I have this problem.

As shown below is my typical volley request. This is to get rows from a table and display them in a recyclerview with cardviews.

I have put the whole process in a method for easy recalling. I call the method in a 'Refresh' button in the toolbar. When I happen to click it more than once before the first request is not yet done the cardviews duplicate as if the rows were requested twice. Well, the method was called twice. I thought if I put new ArrayList<>(); thing during the start of the method should solve it but I was wrong.

Here is what I think happens.

1. load() is called. 2. new instance of myList is created. 3. first request is sent. 4. load() is called again. 5. new instance of myList is created. 6. second request is sent. 7. first request returns json. 8. parses and adds to second myList instance. 9. second request returns json. 10. parses and adds to myList instance.

Here is my typical volley request.

// List<Objectbuilder> myList; is declared in class.


private void load(){
    myLIST= new ArrayList<>();
    RequestQueue requestQueue = Volley.newRequestQueue(TeacherEditQuizQuestionList.this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("RESULT");

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    String tempCOL1 = jsonObject1 .getString("COL1");
                    String tempCOL2 = jsonObject1 .getString("COL2");
                    String tempCOL3 = jsonObject1 .getString("COL3");

                    Objectbuilder newobject = newObjectbuilder(tempCOL1 ,tempCOL2 ,tempCOL3 );

                    myLIST.add(ql_newobject );
                }
                adapter recycler_adapter = new adapter(Activity.this,myLIST);
                recycler_display.setAdapter(ql_adapter );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

                Toast.makeText(Activity.this, ""+error, Toast.LENGTH_SHORT).show();
        }
    })
    };

    requestQueue.add(stringRequest);
}

That method is applied on OnCreate and a "Refresh" button on the toolbar. If I click the refresh button too fast or repeatedly it the cardviews appear as

        [Row1 info            ]
        [Row2 info            ]
        [Row3 info            ]
        [Row1 info            ]
        [Row2 info            ]
        [Row3 info            ]

I mean, its not that much of a bother. If I click the refresh button once and wait patiently it would reset itself and appear normally with the correct number of rows and with no repetition. I just went here just in case there might be a much better way to do this. Perhaps I might learn a life saving trick from you guys that might save this project and this semester lol.

RFA
  • 771
  • 1
  • 5
  • 14

1 Answers1

0

Create a list local to onResponse and populate that list with your response data. When fully populated and ready to apply to your adapter, you then create or clear the myLIST and fill it in. Both outstanding load calls will allocate the do this at the point where they need it (within the onResponse handler). The Response handlers shouldn't preempt each other and the last one to allocate (or clear) myLIST wins. If there is any chance of the Response handlers preempting each other, then add some synchronization (but shouldn't be necessary).

Les
  • 10,335
  • 4
  • 40
  • 60