我正在使用volley来接收来自数据源的数据,如果我在查询上执行某些操作(例如添加参数它不起作用),那么问题就在于回收器视图。 Volley有任何问题吗?
public void getting locations() {
String url = "URL REMOVED";
RequestQueue queue = Volley.newRequestQueue(MainActivity.this); // this = context
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject status = null;
try {
JSONObject jObj = new JSONObject(String.valueOf(response));
JSONArray jr = jObj.getJSONArray("products");
for(int i=0;i<jr.length();i++)
{
JSONObject jb1 = jr.getJSONObject(i);
String title = jb1.getString("title");
Log.d("Title",""+title);
String img = jb1.getString("featured_src");
JSONArray a = jb1.getJSONArray("categories");
String category = "";
lstBook.add(new Book(title,"rAJA","Description book",img));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("EROOOOOOOOOOOOOOOOOOOOOR", "Error: " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
}
问题是,如果URL是产品,它将显示数据,否则不会。
答案 0 :(得分:0)
将myAdapter声明为私有变量并更改下面提到的代码。
在myAdapter.notifyDataSetChanged();
和lstBook.add(new Book(title,"rAJA","Description book",img));
myrv.setAdapter(myAdapter);
行
for(int i=0;i<jr.length();i++)
{
JSONObject jb1 = jr.getJSONObject(i);
String title = jb1.getString("title");
String img = jb1.getString("featured_src");
JSONArray a = jb1.getJSONArray("categories");
String category = "";
lstBook.add(new Book(title,"rAJA","Description book",img));
myAdapter.notifyDataSetChanged();
}
和
myAdapter = new RecyclerViewAdapter(this,lstBook);
myrv.setLayoutManager(new GridLayoutManager(this,2));
myrv.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();