RecyclerView没有数据

时间:2016-04-22 09:18:44

标签: android android-recyclerview recycler-adapter

我使用rretrofit库从服务器获取数据。尝试记录,数据成功给我。但在RecyclerView我没有任何数据。在videoview内,我只看到一个黑色数字,txtview都是空的。我如何将这些数据传输到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>

1 个答案:

答案 0 :(得分:0)

您的列表是否包含项目,但项目未显示其内容?所以,请检查项目视图。如果您的列表没有项目,请检查您的适配器构造函数或日志itemcount