Why the firebase data doesn't appear in the recycler view? - android

the firebase data doesn't appear in the recycler view
i have some realtimeDb in Google firebase
i try to read it in the recyclerView but nothing appears ,
logcat shows null and here is my code:
1-Here is HomeFragment(Main) Code
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
Unbinder unbinder;
#BindView(R.id.recycler_popular)
RecyclerView recyclerView;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
unbinder= ButterKnife.bind(this,root);
init();
homeViewModel.getPopularList().observe(this, new Observer<List<PopularCategoryModel>>() {
#Override
public void onChanged(List<PopularCategoryModel> PopularCategoryModel) {
//create adapter
MyPopularCategoriesAdapter adapter = new MyPopularCategoriesAdapter(HomeFragment.this.getContext(), PopularCategoryModel);
recyclerView.setAdapter(adapter);
}
});
return root;
}
private void init() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
2-Here is HomeViewModel Code
public class HomeViewModel extends ViewModel implements IPopularCallbackListener {
private static final String TAG = "HomeViewModel";
private MutableLiveData<List<PopularCategoryModel>> popularList;
private MutableLiveData<String> messageError;
private IPopularCallbackListener popularCallbackListener;
public HomeViewModel() {
}
public MutableLiveData<List<PopularCategoryModel>> getPopularList() {
if (popularList== null){
popularList=new MutableLiveData<>();
messageError=new MutableLiveData<>();
loadPopularList();
}
return popularList;
}
private void loadPopularList() {
final List<PopularCategoryModel> tempList = new ArrayList<>();
DatabaseReference popularRef= FirebaseDatabase.getInstance().getReference(Common.POPULAR_CATEGORY_REF);
popularRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
tempList.clear();
for (DataSnapshot itemSnapShot:dataSnapshot.getChildren()){
PopularCategoryModel model=itemSnapShot.getValue(PopularCategoryModel.class);
tempList.add(model);
Log.d(TAG, "onDataChange:Adel2020"+tempList.size()+popularRef);
}
//popularCallbackListener.onPopularLoadSuccess(tempList);
Log.e(TAG, "Data received:" + tempList.size());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
popularCallbackListener.onPopularLoadFailed(databaseError.getMessage());
}
});
}
public MutableLiveData<String> getMessageError() {
return messageError;
}
#Override
public void onPopularLoadSuccess(List<PopularCategoryModel> popularCategoryModels) {
popularList.setValue(popularCategoryModels);
}
#Override
public void onPopularLoadFailed(String message) {
messageError.setValue(message);
}
}
3- This is the Adapter
public class MyPopularCategoriesAdapter extends RecyclerView.Adapter<MyPopularCategoriesAdapter.PopViewHolder> {
private List<PopularCategoryModel> listPopularCategoryModel ;
Context context;
public MyPopularCategoriesAdapter( Context context,List<PopularCategoryModel> listPopularCategoryModel) {
this.listPopularCategoryModel = listPopularCategoryModel;
this.context = context;
}
#NonNull
#Override
public PopViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new PopViewHolder(LayoutInflater.from(parent.getContext()).inflate
(R.layout.layout_popular_categories_item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull PopViewHolder holder, int position) {
Glide.with(context).load(listPopularCategoryModel.get(position).getImage()).into(holder.popCategoryImage);
holder.popCategoryName.setText(listPopularCategoryModel.get(position).getName());
}
#Override
public int getItemCount() {
return listPopularCategoryModel.size();
}
public class PopViewHolder extends RecyclerView.ViewHolder {
Unbinder unbinder;
#BindView(R.id.pop_category_name)
TextView popCategoryName;
#BindView(R.id.pop_category_image)
CircleImageView popCategoryImage;
public PopViewHolder(#NonNull View itemView) {
super(itemView);
unbinder= ButterKnife.bind(this,itemView);
}
}
}
4-This is Model
public class PopularCategoryModel {
private String menu_id,food_id,name,image;
public PopularCategoryModel() {
}
public PopularCategoryModel(String menu_id, String food_id, String name, String image) {
this.menu_id = menu_id;
this.food_id = food_id;
this.name = name;
this.image = image;
}
public PopularCategoryModel(String name, String image) {
this.name = name;
this.image = image;
}
+SetterGetter
5-Here is the Data

Related

How to get onclick item in an android recyclerview and send its attributes to a new activity?

I'm developing an android app for online order. User needs to click on an food item in recyclerView and I'm trying to send the food ID to the AddOrderActivity. How can I get the FId and pass it to the new activity?
CustomerHomeFragment.java
public class CustomerHomeFragment extends Fragment {
private RecyclerView recyclerView;
private FoodsAdapter adapter;
private List<Food> foodList;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home_customer, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Call<FoodsResponse> call = RetrofitClient.getInstance().getApi().getFoods();
call.enqueue(new Callback<FoodsResponse>() {
#Override
public void onResponse(Call<FoodsResponse> call, Response<FoodsResponse> response) {
foodList = response.body().getFoods();
adapter = new FoodsAdapter(getActivity(), foodList);
recyclerView.setAdapter(adapter);
}
#Override
public void onFailure(Call<FoodsResponse> call, Throwable t) {
}
});
}
}
public class FoodsAdapter extends RecyclerView.Adapter implements View.OnClickListener{
private Context mCtx;
private List<Food> foodList;
public FoodsAdapter(Context mCtx, List<Food> foodList) {
this.mCtx = mCtx;
this.foodList = foodList;
}
#NonNull
#Override
public FoodsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_foods, parent, false);
return new FoodsViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull FoodsViewHolder holder, int position) {
Food food = foodList.get(position);
holder.textViewFName.setText(food.getFName());
holder.textViewUnitPrice.setText(String.format("%d", food.getUnitPrice()));
holder.textViewAvailCount.setText(String.format("%d", food.getAvailCount()));
}
#Override
public int getItemCount() {
return foodList.size();
}
//////////////
#Override
public void onClick(View view) {
Intent intent = new Intent(this.mCtx, AddOrderActivity.class);
this.mCtx.startActivity(intent);
}
/////////////////
class FoodsViewHolder extends RecyclerView.ViewHolder {
TextView textViewFName, textViewUnitPrice, textViewAvailCount;
public FoodsViewHolder(View itemView) {
super(itemView);
textViewFName = itemView.findViewById(R.id.textViewFName);
textViewUnitPrice = itemView.findViewById(R.id.textViewUnitPrice);
textViewAvailCount = itemView.findViewById(R.id.textViewAvailCount);
}
}
}
public class Food {
private int fid, unitprice, availcount;
private String fname;
public Food(int fid, String fname, int unitprice, int availcount) {
this.fid = fid;
this.fname = fname;
this.unitprice = unitprice;
this.availcount = availcount;
}
public int getFId() {
return fid;
}
public String getFName() {
return fname;
}
public int getUnitPrice() {
return unitprice;
}
public int getAvailCount() {
return availcount;
}
}
in the your adapter class inside the onBindViewHolder function add this code
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,NoteActivity.class);
intent.putExtra("note_id", currentNote.getNoteId());
intent.putExtra("note_name",currentNote.getNoteName());
intent.putExtra("note_teacher",currentNote.getNoteTeacher());
intent.putExtra("note_university",currentNote.getNoteUniversity());
intent.putExtra("note_image",currentNote.getNoteImage());
intent.putExtra("note_is_downloaded", currentNote.getIsDownloaded());
intent.putExtra("note_storage_path", currentNote.getStoragePath());
intent.putExtra("note_download_link", currentNote.getNoteDownloadLink());
context.startActivity(intent);
}
});

How to filter RecyclerView data in Fragment

I do not know how to filter results from RecyclerView which is being displayed in Fragment.
If possible, I want to merge some of these codes into one java file.
i have searched on Google for some examples, but most of them are implemented in Activity instead.
My fragment code :
public class SearchFragment extends Fragment {
private RecyclerView recyclerView;
private AdvertiseAdapter advertiseAdapter;
private List<Advertise> advertiseList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search,container,false);
recyclerView = view.findViewById(R.id.searchResult_view);
new DatabaseHelper().readProperty(new DatabaseHelper.DataStatus() {
#Override
public void DataIsLoaded(List<Advertise> advertiseList, List<String> keys) {
new RecyclerViewConfig().setConfig(recyclerView,getContext(),advertiseList,keys);
}
});
return view;
}
My DatabaseHelper
public class DatabaseHelper {
private FirebaseDatabase firebaseDatabase;
private DatabaseReference reference;
private List<Advertise> property = new ArrayList<>();
public interface DataStatus{
void DataIsLoaded(List<Advertise> ads,List<String> keys);
}
public DatabaseHelper() {
firebaseDatabase = FirebaseDatabase.getInstance();
reference = firebaseDatabase.getReference("Property");
}
public void readProperty(final DataStatus dataStatus){
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
property.clear();
List<String> propertyKeys = new ArrayList<>();
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()){
for (DataSnapshot propertySnapshot : userSnapshot.getChildren()){
propertyKeys.add(propertySnapshot.getKey());
Advertise advertise = propertySnapshot.getValue(Advertise.class);
property.add(advertise);
}
}
dataStatus.DataIsLoaded(property,propertyKeys);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
My RecyclerViewConfig code :
public class RecyclerViewConfig {
private Context mContext;
private PropertyAdapter propertyAdapter;
public void setConfig(RecyclerView recyclerView, Context context, List<Advertise> advertiseList, List<String> keys){
mContext = context;
propertyAdapter = new PropertyAdapter(advertiseList,keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(propertyAdapter);
}
class PropertyItemView extends RecyclerView.ViewHolder{
//private ImageView PropertyImage;
private TextView PropertyName;
private TextView PropertyPostal;
private TextView PropertyState;
private TextView PropertyDetail;
private String key;
public PropertyItemView(ViewGroup parent){
super(LayoutInflater.from(mContext)
.inflate(R.layout.search_view,parent,false));
//PropertyImage = itemView.findViewById(R.id.advertiseImage);
PropertyName = itemView.findViewById(R.id.advertiseName);
PropertyState = itemView.findViewById(R.id.advertiseState);
PropertyPostal = itemView.findViewById(R.id.advertisePostal);
PropertyDetail = itemView.findViewById(R.id.advertiseDetail);
}
public void bind(Advertise advertise,String key){
//PropertyImage.setImageDrawable(advertise.getProperty_imageUrl());
PropertyName.setText(advertise.getName());
PropertyState.setText(advertise.getState());
PropertyPostal.setText(advertise.getPostal());
PropertyDetail.setText(advertise.getDetail());
this.key = key;
}
}
class PropertyAdapter extends RecyclerView.Adapter<PropertyItemView>{
private List<Advertise> advertiseList;
private List<String> mkeys;
public PropertyAdapter(List<Advertise> advertiseList, List<String> mkeys) {
this.advertiseList = advertiseList;
this.mkeys = mkeys;
}
#NonNull
#Override
public PropertyItemView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new PropertyItemView(parent);
}
#Override
public void onBindViewHolder(#NonNull PropertyItemView holder, int position) {
holder.bind(advertiseList.get(position),mkeys.get(position));
final Advertise ads = advertiseList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent redirect = new Intent(mContext,PropertyActivity.class);
redirect.putExtra("propertyID", ads.getPropertyID());
mContext.startActivity(redirect);
}
});
}
#Override
public int getItemCount() {
return advertiseList.size();
}
public void updateList(List<String> newList){
mkeys = new ArrayList<>();
mkeys.addAll(newList);
notifyDataSetChanged();
}
}

RecyclerView not showing data until turn off and turn on screen

I am developing an app that shows in a RecyclerView an image and a text that comes from firebase.
When I open the application the recyclerview does not appear but if I turn off and then turn on the screen the recyclerview appears correctly with the image and text obtained from firebase. I need help please
ViewHolder:
public class ViewHolderServicio extends RecyclerView.ViewHolder {
View mView;
public ViewHolderServicio(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String nombreServicio, String imagenPerfil) {
TextView mNombreServicio = mView.findViewById(R.id.NombreServicio);
ImageView mImagenPerfil = mView.findViewById(R.id.imageViewServicio);
mNombreServicio.setText(nombreServicio);
Picasso.get().load(imagenPerfil).into(mImagenPerfil);
}
}
Model:
public class ModelServicio {
String idUsuario, nombreServicio, descripcion, imagenPerfil;
public ModelServicio(){}
public String getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(String idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombreServicio() {
return nombreServicio;
}
public void setNombreServicio(String nombreServicio) {
this.nombreServicio = nombreServicio;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getImagenPerfil() {
return imagenPerfil;
}
public void setImagenPerfil(String imagenPerfil) {
this.imagenPerfil = imagenPerfil;
}
}
class activity:
public class DetalleServicio extends AppCompatActivity {
RecyclerView RVbot;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<ModelServicio> options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle_servicio);
RVbot = findViewById(R.id.RVBot);
RVbot.setHasFixedSize(true);
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Servicios");
mostrarDatos();
}
private void mostrarDatos() {
options = new FirebaseRecyclerOptions.Builder<ModelServicio>().setQuery(mRef, ModelServicio.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolderServicio holder, int position, #NonNull ModelServicio model) {
holder.setDetails(getApplicationContext(), model.getNombreServicio(), model.getImagenPerfil());
}
#NonNull
#Override
public ViewHolderServicio onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.servicioitem, viewGroup, false);
ViewHolderServicio viewHolder = new ViewHolderServicio(itemView);
return viewHolder;
}
};
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
RVbot.setAdapter(firebaseRecyclerAdapter);
}
}
Have you tried
firebaseRecyclerAdapter.notifyDataSetChanged();
after calling an Adapter?

RecyclerView In Fragment Not Working (Unable to add data to it)

Here is my fragment
public class TransactionsFragment extends Fragment {
private FirebaseAuth mAuth;
private DatabaseReference mDBref;
FirebaseUser Fuser;
private String UID;
private RecyclerView recyclerView;
private RecyclerView.Adapter trpAdapter;
private List<TransGetterSetter> DataList;
String trans_id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_transactions, container, false);
DataList = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.RVList);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
TransactionRVAdapter trvAdapter = new TransactionRVAdapter(DataList, getContext());
recyclerView.setAdapter(trvAdapter);
mAuth = FirebaseAuth.getInstance();
mDBref = FirebaseDatabase.getInstance().getReference();
Fuser = mAuth.getInstance().getCurrentUser();
UID = Fuser.getUid();
mDBref.child("transactions").child(UID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
trans_id = chidSnap.getKey();
DataList.add(new TransGetterSetter("" + trans_id));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
trvAdapter.notifyDataSetChanged();
return v;
}
}
If i toast "trans_id" string im getting expected result.
Here is My Adapter class
public class TransactionRVAdapter extends RecyclerView.Adapter<TransactionRVAdapter.rvViewHolder> {
Context mContext;
List<TransGetterSetter> DataList = new ArrayList<>();
public class rvViewHolder extends RecyclerView.ViewHolder{
private TextView TransId;
public rvViewHolder(#NonNull View itemView) {
super(itemView);
TransId = (TextView) itemView.findViewById(R.id.TransIdTxt);
}
}
public TransactionRVAdapter( List<TransGetterSetter> DataList,Context mContext){
this.DataList = DataList;
this.mContext = mContext;
}
#NonNull
#Override
public rvViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.fragment_transactions_item, viewGroup, false);
return new rvViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull rvViewHolder rvViewHolder, int i) {
rvViewHolder.TransId.setText(DataList.get(i).getTrans_id());
}
#Override
public int getItemCount() {
return DataList.size();
}
}
Here is my GetterSetter class
public class TransGetterSetter {
private String trans_id;
public TransGetterSetter() {
}
public TransGetterSetter(String trans_id) {
this.trans_id = trans_id;
}
public String getTrans_id() {
return trans_id;
}
public void setTrans_id(String trans_id) {
this.trans_id = trans_id;
}
}
Im able to get Data from firebase, but when i try to add it to recycler view it dosen't show any data. Recycler view is showing empty. I checked my code many time but haven't found solution.
You should call :
trvAdapter.notifyDataSetChanged();
After the for loop inside the (onDataChange) event listener.
Try to called ..
DataList.add(new TransGetterSetter("" + trans_id));
after this line called ..
trvAdapter.notifyDataSetChanged();

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()
Your VideoViewAdapter should know how many items he should show, for that purposes you need to provide the size of your data variable, through getItemCount() method:
#Override
public int getItemCount() {
return data.size();
}

Categories

Resources