RecyclerView의 어댑터를 업데이트하기 전에 Volley의 응답을 기다리는 데 문제가 있습니다. 요청이있는 OnResponse
메서드를 알고 있지만 매개 변수로 어댑터를 전달할 방법이 없습니다. Volley가 대답을 제공했다는 것을 내 어댑터에 알리는 쉬운 방법이 있습니까?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();
}
}
}
당신이 루프에 대한 후를 추가해야 onResponse 메소드 내에서 어댑터를 참조 할 수의 멤버 변수 만들기 - for (int i = 0; i
Tasos