2016-08-22 3 views
0

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(); 
     } 
    } 
} 
+0

당신이 루프에 대한 후를 추가해야 onResponse 메소드 내에서 어댑터를 참조 할 수의 멤버 변수 만들기 - for (int i = 0; i Tasos

답변

1

I 내부에 재활용 어댑터를 설정 정지

어댑터를 다음과 같이 전달할 방법이 없습니다. 그것에 대한 매개 변수.

실제로 두 가지 방법이 있습니다.

  1. 발리 요청을 둘러싼 방법의 매개 변수를 final 매개 변수로 만듭니다. 즉, 정적 필드에서 메소드로 요청을 이동하십시오. 것을 시도 -
  2. 보다 바람직한, 어댑터 클래스

두 가지 방법이 당신이

0

은 그냥 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(); 
     } 
    } 
} 
관련 문제