在更新我的RecyclerView适配器之前,我无法等待Volley的响应。我知道请求有OnResponse
方法,但是,我无法将适配器作为参数传递给它。有没有一种简单的方法可以通知我的适配器Volley提供了答案?
private static JsonObjectRequest jsonReq = new JsonObjectRequest
(Request.Method.GET, espUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (espVideos != null) {
espVideos.clear();
}
JSONArray jsonArray = response.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.getJSONObject(i).getJSONObject("contentDetails").getString("videoId");
String title = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("title");
espVideo = new YTVideo(id, title);
espVideos.add(espVideo);
}
SaveObject.saveYTVArray("espVideos", espVideos);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
public static void requestEspeciales(Activity act, ImageAdapterEspeciales adapter) {
Volley.newRequestQueue(act).add(jsonReq);
// I WANT TO CALL adapter.notifyDataSetChanged(); AFTER RESPONSE
}
编辑:
我尝试了同步调用,但页面只是冻结了onCreate
synchronized (jsonReq) {
try {
jsonReq.wait();
} catch (InterruptedException e) {
Log.e("Error", e.toString());
}
Log.d("Notifying", espVideo.toString());
adapter.notifyDataSetChanged();
}
@Override
public void onResponse(JSONObject response) {
synchronized (jsonReq) {
try {
if (espVideos != null) {
espVideos.clear();
}
JSONArray jsonArray = response.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.getJSONObject(i).getJSONObject("contentDetails").getString("videoId");
String title = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("title");
String description = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("description");
String thumbnailPath = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("high").getString("url");
String correspondingPlaylist = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("playlistId");
espVideo = new YTVideo(id, title, description, thumbnailPath, correspondingPlaylist);
espVideos.add(espVideo);
}
SaveAndRetrieve.saveYTVArray("espVideos", espVideos);
Log.d("Retrieving", ((YTVideo) SaveAndRetrieve.getYTVArray("espVideos").get(0)).title);
notify();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
只需在您的volleyresponse
中设置您的回收器适配器@Override
public void onResponse(JSONObject response) {
synchronized (jsonReq) {
try {
if (espVideos != null) {
espVideos.clear();
}
JSONArray jsonArray = response.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.getJSONObject(i).getJSONObject("contentDetails").getString("videoId");
String title = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("title");
String description = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("description");
String thumbnailPath = jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("high").getString("url");
String correspondingPlaylist = jsonArray.getJSONObject(i).getJSONObject("snippet").getString("playlistId");
espVideo = new YTVideo(id, title, description, thumbnailPath, correspondingPlaylist);
espVideos.add(espVideo);
}
SaveAndRetrieve.saveYTVArray("espVideos", espVideos);
Log.d("Retrieving", ((YTVideo) SaveAndRetrieve.getYTVArray("espVideos").get(0)).title);
**YourAdapaterClass mAdapter = new YourAdapaterClass(youradapterdata);
recyclerview.setAdapter(mAdapter);**
notify();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:1)
我无法将适配器作为参数传递给它。
你实际上有两种方式。
final
参数。换句话说,将请求从静态字段移动到方法中。 两种方式都允许您从onResponse方法
中引用适配器