0

我的RecyclerView适配器被声明为

 adapterlist = new AdapterList(getActivity(),getData());

getData()函数定义为

public List<Information> getData(){
    final List<Information> data = new ArrayList<>();
    showpDialog();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, urlfood, null, new Response.Listener<JSONObject>() {


                @Override
                public void onResponse(JSONObject jsonObject) {
                    for (int i = 0; i < 10; i++) {
                    try {


                        JSONArray items = jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items");



                           if (jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").has("location")) {
                               if(jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").getJSONObject("location").has("address")){

                                JSONObject location = jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").getJSONObject("location");
                                Address[i] = location.getString("address");}}

                            Name[i] = items.getJSONObject(i).getJSONObject("venue").getString("name");
                            String rtg=items.getJSONObject(i).getJSONObject("venue").getString("rating");
                            Rating[i]=Float.parseFloat(rtg);
                            Information current = new Information();
                            current.name = Name[i];

                            current.address = Address[i];
                            current.ratings = Rating[i];
                            data.add(current);


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
                    recyclerView.setAdapter(adapterlist);
                    hidepDialog();

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(),
                            "error:"+error.getMessage(), Toast.LENGTH_SHORT).show();

                   hidepDialog();
                }
            }


            );
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);


    return data;
}

这些东西是在扩展片段的类中定义的。如何缓存原始响应或解析的响应?请帮忙。

我已经在 StackOverflow 上检查了许多答案,但我无法理解如何将它们集成到我的片段类中。

4

0 回答 0