2016-04-22 94 views
0

我用rretrofit庫從服務器獲取數據。嘗試記錄,數據成功給我。但在RecyclerView我沒有任何數據。裏面videoview我看到的只是一個黑色的身影和兩個txtview是空的。我如何將這些數據轉移到recyclerview? 我的片段與響應:RecyclerView沒有數據

public class FeaturedFragment extends Fragment { 
    RecyclerViewAdapter recyclerViewAdapter; 
    public static final String ROOT_URL = "https://api.vid.me/"; 
    public List<Video> videos; 
    RecyclerView recList; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_featured, container, false); 
     recList = (RecyclerView) rootView.findViewById(R.id.cardList); 
     recList.setHasFixedSize(true); 
     LinearLayoutManager llm = new LinearLayoutManager(getActivity()); 
     llm.setOrientation(LinearLayoutManager.VERTICAL); 
     recList.setLayoutManager(llm); 
     try { 
      getVideos(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return rootView; 
    } 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 

    private void getVideos() throws IOException { 
     Retrofit retrofitAdapter = new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(ROOT_URL) 
       .build(); 
     final VideoApi videoApi = retrofitAdapter.create(VideoApi.class); 
     Call<Videos> call = videoApi.getFeaturedVideo(); 
     call.enqueue(new Callback<Videos>() { 
      @Override 
      public void onResponse(Call<Videos> call, Response<Videos> response) { 
       Log.d("MainActivity", "Status Code = " + response.code()); 
       Log.d("MainActivity", "Status Code = " + response.body().videos.get(1).getTitle()); 
       Log.d("MainActivity", "Status Code = " + response.body().videos.get(1).getScore()); 
       Log.d("MainActivity", "Status Code = " + response.body().videos.get(1).getClipUrl()); 
       videos = response.body().videos; 
       recyclerViewAdapter = new RecyclerViewAdapter(videos); 


       recList.setAdapter(recyclerViewAdapter); 
      } 

      @Override 
      public void onFailure(Call<Videos> call, Throwable t) { 

      } 


     }); 
    } 

} 

RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.VideoViewHolder> { 
    List<Video> call; 
    RecyclerViewAdapter(List<Video> call){ 
     this.call = call; 

    } 
    @Override 
    public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row,parent,false); 
     VideoViewHolder videoViewHolder = new VideoViewHolder(v); 
     return videoViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(VideoViewHolder holder, int position) { 
     String video_url = call.get(position).getClipUrl(); 
holder.videoView.setVideoURI(Uri.parse(video_url)); 

     holder.video_name.setText(call.get(position).getTitle()); 
     // holder.video_like.setText(call.get(position).getScore()); 
    } 

    @Override 
    public int getItemCount() { 
     return call.size(); 
    } 

    public class VideoViewHolder extends RecyclerView.ViewHolder { 
     CardView cardView; 
     VideoView videoView; 
     TextView video_name; 
     TextView video_like; 
     public VideoViewHolder(View itemView) { 
      super(itemView); 
      cardView = (CardView) itemView.findViewById(R.id.card_view); 
      videoView = (VideoView)itemView.findViewById(R.id.videoview); 
      video_name = (TextView) itemView.findViewById(R.id.Videoname_textView); 
      video_like = (TextView) itemView.findViewById(R.id.like_textview); 
     } 
    } 
} 

card_view_row.xml:

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="180dp" 
    card_view:cardCornerRadius="4dp" 
    android:layout_margin="5dp"> 
<RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="130dp" 
    android:padding="10dp" 
    > 
<VideoView 

    android:layout_width="match_parent" 
    android:layout_height="130dp" 
    android:id="@+id/videoview" 
    /> 
    <TextView 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/Videoname_textView" 
     android:text="NameofVideo" 
     android:layout_below="@id/videoview" 
     android:layout_marginTop="4dp"> 

    </TextView> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/like_textview" 
     android:text="Likes" 
     android:layout_alignRight="@id/videoview" 
     android:layout_below="@id/videoview" 
     android:layout_marginTop="4dp" /> 
</RelativeLayout> 
    </android.support.v7.widget.CardView> 

回答

0

請問您的列表是有項目,但項目沒有顯示其內容?所以,請檢查項目視圖。如果您的列表中有沒有項目,請檢查您適配器的構造函數或登錄ITEMCOUNT

+0

我檢查與日誌項片段onResonse方法,並在那個地方我有數據,但是當我把它適配器,也許我在錯誤的發送方式,因爲我沒有在RecyclerView中的數據。 –