android - ReyclerView no funciona
android-recyclerview recycler-adapter (6)
Estoy tratando de mostrar la lista de mi modelo en la vista de reciclaje. pero por alguna razón no muestra nada. Recibo los elementos de mi lista de FirebaseDatabase y guardo todos los datos en "videoList". Entonces quiero presentar los datos que he recibido, pero no muestra nada.
Por favor dime que estoy haciendo mal.
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);
}
}
}
Debe
return
el
size
de su ArrayList de
data
en
getItemCount()
Utilizar esta:
@Override
public int getItemCount() {
return data.size();
}
En lugar de esto:
@Override
public int getItemCount() {
return 0;
}
Estás usando el siguiente código
@Override
public int getItemCount() {
return 0;
}
Estás devolviendo cero. Esta es la razón por la que no se muestra nada. Necesita devolver el tamaño de la lista de datos. en su caso debe ser videosList.size ()
Por favor cambia tu código
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);
}
}
}
Prueba esto
@Override
public int getItemCount() {
return data.size();
}
Su
VideoViewAdapter
debe saber cuántos elementos debe mostrar, para eso necesita proporcionar el tamaño de su variable de
data
, a través del método
getItemCount()
:
@Override
public int getItemCount() {
return data.size();
}
Su problema será resuelto.
Use este código en su
adapter
:
@Override
public int getItemCount() {
return data.size();
}