ReyclerView isn't working
I'm trying to show list of my model on recyclerview. but for some reason it doesnt show anything. I'm getting my list items from FirebaseDatabase and save all the data on "videoList". Then i want to presents the data that i have received, but it doesnt show anything.
Please tell me what i'm doing wrong.
public class MyPlayListFragment extends Fragment {
private FirebaseDatabase refToVideos;
private FirebaseUser currentUser;
private ArrayList<Video> videosList;
private VideoViewAdapter adapter;
private RecyclerView rvVideos;
public MyPlayListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_my_play_list, container, false);
rvVideos = (RecyclerView)v.findViewById(R.id.rvVideos);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
videosList = new ArrayList<>();
refToVideos = FirebaseDatabase.getInstance();
currentUser = FirebaseAuth.getInstance().getCurrentUser();
refToVideos.getReference(Params.VIDEOS).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
videosList.add(snapshot.getValue(Video.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter = new VideoViewAdapter(getContext(),videosList);
rvVideos.setLayoutManager(new LinearLayoutManager(getContext()));
rvVideos.setAdapter(adapter);
}
public class VideoViewAdapter extends RecyclerView.Adapter<VideoViewHolder>{
private LayoutInflater inflater;
private Context context;
private ArrayList<Video> data;
public VideoViewAdapter( Context context, ArrayList<Video> data) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.video_item,parent,false);
return new VideoViewHolder(v);
}
@Override
public void onBindViewHolder(VideoViewHolder holder, int position) {
Video video = data.get(position);
holder.tvTitleVideo.setText(video.getVideoName());
holder.tvLikes.setText(String.valueOf(video.getLikes()));
holder.tvViews.setText(String.valueOf(video.getViews()));
Uri uri = Uri.parse(video.getVideoURL());
holder.videoViewUser.setVideoURI(uri);
holder.videoViewUser.start();
}
@Override
public int getItemCount() {
return 0;
}
}
public class VideoViewHolder extends RecyclerView.ViewHolder{
private TextView tvTitleVideo,tvLikes,tvViews;
private VideoView videoViewUser;
public VideoViewHolder(View itemView) {
super(itemView);
tvTitleVideo = (TextView) itemView.findViewById(R.id.videoTitle);
tvLikes = (TextView)itemView.findViewById(R.id.tvLikes);
tvViews = (TextView)itemView.findViewById(R.id.tvViews);
videoViewUser = (VideoView)itemView.findViewById(R.id.videoViewUser);
}
}
}
You should return
the size
of your data
ArrayList in getItemCount()
Use this:
@Override
public int getItemCount() {
return data.size();
}
Instead of this:
@Override
public int getItemCount() {
return 0;
}
Try this
@Override
public int getItemCount() {
return data.size();
}
Please change your code
getitemcount()
{
return 0:
}
to
videosList.size();
public class MyPlayListFragment extends Fragment {
private FirebaseDatabase refToVideos;
private FirebaseUser currentUser;
private ArrayList<Video> videosList;
private VideoViewAdapter adapter;
private RecyclerView rvVideos;
public MyPlayListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_my_play_list, container, false);
rvVideos = (RecyclerView)v.findViewById(R.id.rvVideos);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
videosList = new ArrayList<>();
refToVideos = FirebaseDatabase.getInstance();
currentUser = FirebaseAuth.getInstance().getCurrentUser();
refToVideos.getReference(Params.VIDEOS).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
videosList.add(snapshot.getValue(Video.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter = new VideoViewAdapter(getContext(),videosList);
rvVideos.setLayoutManager(new LinearLayoutManager(getContext()));
rvVideos.setAdapter(adapter);
}
public class VideoViewAdapter extends RecyclerView.Adapter<VideoViewHolder>{
private LayoutInflater inflater;
private Context context;
private ArrayList<Video> data;
public VideoViewAdapter( Context context, ArrayList<Video> data) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.video_item,parent,false);
return new VideoViewHolder(v);
}
@Override
public void onBindViewHolder(VideoViewHolder holder, int position) {
Video video = data.get(position);
holder.tvTitleVideo.setText(video.getVideoName());
holder.tvLikes.setText(String.valueOf(video.getLikes()));
holder.tvViews.setText(String.valueOf(video.getViews()));
Uri uri = Uri.parse(video.getVideoURL());
holder.videoViewUser.setVideoURI(uri);
holder.videoViewUser.start();
}
@Override
public int getItemCount() {
return "YourList.size()";
}
}
public class VideoViewHolder extends RecyclerView.ViewHolder{
private TextView tvTitleVideo,tvLikes,tvViews;
private VideoView videoViewUser;
public VideoViewHolder(View itemView) {
super(itemView);
tvTitleVideo = (TextView) itemView.findViewById(R.id.videoTitle);
tvLikes = (TextView)itemView.findViewById(R.id.tvLikes);
tvViews = (TextView)itemView.findViewById(R.id.tvViews);
videoViewUser = (VideoView)itemView.findViewById(R.id.videoViewUser);
}
}
}
Your problem will be solved.
Use this code in your adapter
:
@Override
public int getItemCount() {
return data.size();
}
You are using below code
@Override
public int getItemCount() {
return 0;
}
You are returning zero. This is the reason that nothing is shown. You need to return data list size. in Your case it should be videosList.size()