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

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();

Related

About opening a new activity in respond to a click on a RecyclerView item which is inside of a fragment

I'm loading some data into a RecyclerView from Firebase Realtime Database. This RecyclerView is in a fragment and I want to open a new activity when an item is clicked. But when i add the onClickListener to an item from the adapter, my click doesn't even recognized by the app. I'm not sure what I'm doing wrong here because this method worked fine for a RecyclerView when it is inside of a normal activity.
This is a project for my university and I should submit it tomorrow. So I can really use some help.
This is the my data Adapter class,
public class Adapter_NoLimit extends RecyclerView.Adapter<Adapter_NoLimit.LViewHolder> {
Context context;
ArrayList<Helper> list_nl;
public Adapter_NoLimit(Context context, ArrayList<Helper> list_nl) {
this.context = context;
this.list_nl = list_nl;
}
#NonNull
#Override
public Adapter_NoLimit.LViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.itemlist, parent, false);
return new LViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull Adapter_NoLimit.LViewHolder holder, int position) {
//final Helper temp = list_nl.get(position);
String fname_txt = list_nl.get(position).getFname();
String catg_txt = list_nl.get(position).getSpin();
String prof_img = list_nl.get(position).getProfile_url();
holder.setUsers(fname_txt, catg_txt, prof_img);
holder.f_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) v.getContext();
Intent i = new Intent(activity, UserProfile_VV.class);
i.putExtra("full name", fname_txt);
activity.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return list_nl.size();
}
public class LViewHolder extends RecyclerView.ViewHolder{
private TextView f_name, catG;
private ImageView profile_pic;
public LViewHolder(#NonNull View itemView) {
super(itemView);
f_name = itemView.findViewById(R.id.username_view);
catG = itemView.findViewById(R.id.category_view);
profile_pic = itemView.findViewById(R.id.userimage);
}
public void setUsers(String fname_txt, String catg_txt, String prof_img) {
f_name.setText(fname_txt);
catG.setText(catg_txt);
Picasso.get().load(prof_img).fit().into(profile_pic);
}
}
}
This is one of fragments,
public class Beauty_Frag extends Fragment {
RecyclerView recyclerView;
DatabaseReference reference, user_ref;
ArrayList<Helper> list;
Adapter adapter;
FirebaseAuth auth;
String uid;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_beauty, container, false);
recyclerView = v.findViewById(R.id.beauty_recycler_frag);
reference = FirebaseDatabase.getInstance().getReference();
user_ref = reference.child("users");
auth = FirebaseAuth.getInstance();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//searchField = findViewById(R.id.search_field);
list = new ArrayList<>();
adapter = new Adapter(getContext(), list);
recyclerView.setAdapter(adapter);
user_ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull /*#org.jetbrains.annotations.NotNull*/ DataSnapshot snapshot) {
for (DataSnapshot key : snapshot.getChildren()) {
if( key.child("spin").getValue().toString().equals("Beauty")) {
Helper helper = key.getValue(Helper.class);
list.add(helper);
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onCancelled(#NonNull /*#org.jetbrains.annotations.NotNull*/ DatabaseError error) {
}
});
return v;
}
}
You are very welcome if you have any solution.
Use below code for onClicklistner:
holder.f_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, UserProfile_VV.class);
i.putExtra("full name", fname_txt);
context.startActivity(i);
}
});

android recylerview not displaying firebase data

I am trying to retrieve data from another firebase project, but the data is not displaying in the recylerview. I am not getting any errors but just a blank cardview. Can someone help me solve this issue that I am having. My code is below.
// This my code
public class ExampleFragment extends Fragment {
LinearLayout linearLayoutWithoutItems,linearLayoutNoConnection;
View rootView;
private RecyclerView recyclerView;
private Adapter1 aAdapter;
ImageButton menu_click;
ImageView imageView;
//Variables
NavigationView navigationView;
Toolbar toolbar;
Menu menu;
LinearLayout linearLayout;
TextView textView;
BottomNavigationView bottomNavigationView;
private DatabaseReference databaseReference;
private ArrayList<Model_Information> myUploads;
FirebaseStorage firebaseStorage;
StorageReference storageReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView =inflater.inflate(R.layout.fragment_example, container, false);
init();
return rootView;
}
public void init()
{
recyclerView = rootView.findViewById(R.id.WithItems_recyclerview);
linearLayoutWithoutItems = rootView.findViewById(R.id.WithoutItems);
linearLayoutNoConnection = rootView.findViewById(R.id.no_connection);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),1);
recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<Model_Information>();
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
databaseReference = FirebaseDatabase.getInstance("https://liou-43081.firebaseio.com").getReference().child("Post");
if (InternetConnection.checkConnection(getContext())) {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//progressBar.setVisibility(View.GONE);
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
//myUploads.clear();
myUploads.add(upload);
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
aAdapter.notifyDataSetChanged();
}else{
linearLayoutWithoutItems.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
linearLayoutNoConnection.setVisibility(View.VISIBLE);
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
}
//Adapter1.class
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ImageViewHolder>{
private Context mContext;
private ArrayList<Model_Information> users;
DatabaseReference databaseReference;
public Adapter1(Context context, ArrayList<Model_Information> uploads){
mContext = context;
users = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(mContext).inflate(R.layout.cardview1, parent, false);
return new ImageViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
//String uploadCurrent=users.get(position).getmImageUrl();
Glide.with(mContext).load(users.get(position).getmImageUrl()).thumbnail(0.05f).transition(DrawableTransitionOptions.withCrossFade()).fitCenter().centerInside().into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(mContext,users.get(position).getCategory(), Toast.LENGTH_LONG).show();
Long l= Long.valueOf(1);
databaseReference = FirebaseDatabase.getInstance("https://louie-43081.firebaseio.com").getReference("Clicks_and_Views").child(users.get(position).id);
databaseReference.child("views").setValue(ServerValue.increment(l));
/*
Intent intent=new Intent(mContext,ViewActivity.class);
intent.putExtra("website",users.get(position).getWebsiteurl());
intent.putExtra("action",users.get(position).getAction());
intent.putExtra("image",users.get(position).getmImageUrl());
intent.putExtra("id",users.get(position).id);
mContext.startActivity(intent);
*/
}
});
}
#Override
public int getItemCount() {
return users.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image);
}
}
You should not initialize adapter twice.
Your code block should look like this,
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}

E/RecyclerView: No adapter attached; skipping layout on recyclerview inside fragment

I have "E/RecyclerView: No adapter attached; skipping layout" error message.
I have recycleView inside fragment so I must have done something wrong on fragment1.java.
here is the code. I set adapter on the code as you can see, but I am getting this error. Any help would be appreciated. Thank you.
public class Fragment1 extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Bible> arrayList;
private FirebaseDatabase database;
private DatabaseReference databaseReference;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
//GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
arrayList = new ArrayList<>();
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Bible");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Bible bible = snapshot.getValue(Bible.class);
arrayList.add(bible);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.e("fragment1", String.valueOf(databaseError.toException()));//에러문 출력
}
});
adapter = new BibleAdapter(arrayList, getContext());
recyclerView.setAdapter(adapter);
return rootView;
}
}
public class BibleAdapter extends RecyclerView.Adapter {
private ArrayList<Bible> arrayList;
private Context context;
private OnVerseItemClickListener listener;
public BibleAdapter(ArrayList<Bible> arrayList, Context context) {
this.arrayList = arrayList;
this.context = context;
}
public void setOnItemClickListener(OnVerseItemClickListener listener){
this.listener = listener;
}
#NonNull
#Override
public BibleAdapter.BibleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.verse_item, parent, false);
BibleViewHolder holder = new BibleViewHolder(view, listener);
return holder;
}
#Override
public void onBindViewHolder(#NonNull BibleAdapter.BibleViewHolder holder, int position) {
Glide.with(holder.imageView)
.load(arrayList.get(position).getImage())
.into(holder.imageView);
holder.bibleType.setText(arrayList.get(position).getBibleType());
holder.keysentance.setText(arrayList.get(position).getKeysentance());
holder.verse.setText(arrayList.get(position).getVerse());
}
#Override
public int getItemCount() {
return (arrayList!=null? arrayList.size():0);
}
public class BibleViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView bibleType;
TextView verse;
TextView keysentance;
public BibleViewHolder(#NonNull View itemView, final OnVerseItemClickListener listener) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.bibleType = itemView.findViewById(R.id.bibleType);
this.keysentance = itemView.findViewById(R.id.keysentance);
this.verse = itemView.findViewById(R.id.verse);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if(listener != null){
listener.onItemClick(BibleViewHolder.this, v, position);
}
}
});
}
}
}
in your code you have create adapter with empty arrayList, but never after that you update the list.
in adapter create swapData() method and call it when you have to refresh data or have refreshed data, in your case you will get it in onDataChange().
And also define adapter before adding listener:
adapter = new BibleAdapter(arrayList, getContext());
recyclerView.setAdapter(adapter);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Bible bible = snapshot.getValue(Bible.class);
arrayList.add(bible);
}
adapter.swapData(arrayList);
//adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.e("fragment1", String.valueOf(databaseError.toException()));//에러문 출력
}
});
in adapter class create method as:
public void swapData(ArrayList<Bible> filtersList) {
if (filtersList != null) {
this.arrayList.clear();
this.arrayList.addAll(filtersList);
notifyDataSetChanged();
}
}

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();
}
}

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