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();
}
Related
I have a nested recyclerview to show the list of foods in each category. When the user clicks the increase or decrease button, the quantity will be updated to the quantity textview and synced to the cart stored in the database, but currently I don't know how to listen for the event when the user clicks these buttons.
Here is my current UI when run:
enter image description here
Here is my fragment:
public class TabOrderFragment extends Fragment {
private static final String ARG_RESTAURANT_ID = "restaurantId";
private int restaurantId;
View view;
RecyclerView menuRecyclerView;
RecyclerView.LayoutManager layoutManager;
ArrayList<Menu> menuArrayList;
MenuAdapter menuAdapter;
public TabOrderFragment() { }
public static TabOrderFragment newInstance(int restaurantId) {
TabOrderFragment fragment = new TabOrderFragment();
Bundle args = new Bundle();
args.putInt(ARG_RESTAURANT_ID, restaurantId);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
restaurantId = getArguments().getInt(ARG_RESTAURANT_ID);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab_order, container, false);
if(restaurantId != 0) {
DatabaseHandler databaseHandler = DatabaseHandler.getInstance(getActivity());
menuArrayList = new ArrayList<Menu>();
menuArrayList = databaseHandler.getAllMenuFoodsByIdRestaurant(restaurantId);
menuRecyclerView = view.findViewById(R.id.rcv_foods_menu);
menuAdapter = new MenuAdapter(getActivity().getApplicationContext(), restaurantId);
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
menuRecyclerView.setLayoutManager(layoutManager);
menuAdapter.setData(menuArrayList);
menuRecyclerView.setAdapter(menuAdapter);
}
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d("TabOrderFragment", "Restaurant Id = " + restaurantId);
}
Here is my MenuAdapter:
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
Context context;
private int restaurantId;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
ArrayList<Menu> menuList;
public MenuAdapter(ArrayList<Menu> menuList) {
this.menuList = menuList;
}
public MenuAdapter(Context context, int restaurantId) {
this.context = context;
this.restaurantId = restaurantId;
}
public void setData(ArrayList<Menu> menuList) {
this.menuList = menuList;
notifyDataSetChanged();
}
#NonNull
#Override
public MenuViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_foods_menu, parent, false);
return new MenuViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MenuViewHolder holder, int position) {
Menu menu = menuList.get(position);
holder.txtMenuTitle.setText(menu.getTitle().concat(" (" + String.valueOf(menu.getQuantity()) + ")"));
LinearLayoutManager layoutManager = new LinearLayoutManager(holder.rcvListFoods.getContext(), LinearLayoutManager.VERTICAL, false);
layoutManager.setInitialPrefetchItemCount(menu.getFoods().size());
FoodAdapter foodAdapter = new FoodAdapter(menu.getFoods(), restaurantId);
holder.rcvListFoods.setLayoutManager(layoutManager);
holder.rcvListFoods.setAdapter(foodAdapter);
holder.rcvListFoods.setRecycledViewPool(viewPool);
}
#Override
public int getItemCount() {
if(menuList != null) {
return menuList.size();
}
return 0;
}
public class MenuViewHolder extends RecyclerView.ViewHolder {
private TextView txtMenuTitle;
private RecyclerView rcvListFoods;
public MenuViewHolder(#NonNull View itemView) {
super(itemView);
txtMenuTitle = itemView.findViewById(R.id.txv_menu_title);
rcvListFoods = itemView.findViewById(R.id.rcv_list_foods);
}
}
}
Here is my FoodAdapter:
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {
private ArrayList<Food> foodList;
private int restaurantId;
private int userId;
SharedPreferences sharedPreferences;
public FoodAdapter(ArrayList<Food> foodList, int restaurantId) {
this.foodList = foodList;
this.restaurantId = restaurantId;
}
#NonNull
#Override
public FoodViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
sharedPreferences = fragmentActivity.getSharedPreferences("currentUser", Context.MODE_PRIVATE);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_food, parent, false);
return new FoodViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull FoodViewHolder holder, int position) {
Food food =foodList.get(position);
Picasso.get().load(food.getThumbImage()).into(holder.foodThumbImage);
holder.foodName.setText(food.getName());
holder.foodDescription.setText(food.getDescription());
double price = Double.parseDouble(food.getPrice());
DecimalFormat formatter = new DecimalFormat("#,###");
holder.foodPrice.setText(formatter.format(price) + "đ");
}
#Override
public int getItemCount() {
if(foodList != null) {
return foodList.size();
}
return 0;
}
public class FoodViewHolder extends RecyclerView.ViewHolder {
private ImageView foodThumbImage;
private TextView foodName;
private TextView foodDescription;
private TextView foodPrice;
private TextView quantity;
private MaterialButton increaseBtn;
private MaterialButton decreaseBtn;
private ItemClickListener itemClickListener;
public FoodViewHolder(#NonNull View itemView) {
super(itemView);
foodThumbImage = itemView.findViewById(R.id.image_food);
foodName = itemView.findViewById(R.id.txv_food_name);
foodDescription = itemView.findViewById(R.id.txv_food_description);
foodPrice = itemView.findViewById(R.id.txv_food_price);
quantity = itemView.findViewById(R.id.txv_quantity);
increaseBtn = itemView.findViewById(R.id.btn_increase);
decreaseBtn = itemView.findViewById(R.id.btn_decrease);
}
}
in your food adapter's onBindViewHolder method use
holder.increaseBtn.setOnClickListener{
onIncreaseClick()
}
while create an interface like
interface Listener{
public void onIncreaseClick();
public void onDecreaseClick();
}
and implement your interface in your activity or fragment or viewmodel where you can listen for the click event
What you can do generally when you have something like that, is to create an interface for the interaction with the Recycler view:
interface Interaction {
public void onFoodClick(Food food)
}
Then you add an instance of this interface in your recycler view adapter, and you let your activity or fragment implement this interface, please find a good explanation here: https://stackoverflow.com/a/31671289/7334951
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
I am newbie in android ViewModel.I have tried to add arraylist item using viewmodel and recyclerview but I couldn't.Please help me..
This is an GalaryViewModel:
class GalleryViewModel extends ViewModel {
private MutableLiveData<ArrayList<String>>fruits ;
MutableLiveData<ArrayList<String>> getFruits() {
if(fruits == null){
fruits = new MutableLiveData<>();
loadFruits();
}
return fruits;
}
private void loadFruits(){
ArrayList<String> fruitList = new ArrayList<>();
fruitList.add("Apple");
fruitList.add("Banana");
fruitList.add("Pinaple");
fruitList.add("Apple");
fruitList.add("Cucamber");
fruitList.add("Cocunut");
fruits.setValue(fruitList);
}
}
This is an GalleryFragment: Where I could not adding the setText value inside the public void onChanged() Function
public class GalleryFragment extends Fragment {
private Context context;
private View view;
private GalleryViewModel galleryViewModel;
private ArrayList<String> fruitList;
public GalleryFragment(Context context, View view, GalleryViewModel galleryViewModel, ArrayList<String> fruitList) {
this.context = context;
this.view = view;
this.galleryViewModel = galleryViewModel;
this.fruitList = fruitList;
}
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
RecyclerView recyclerView = view.findViewById(R.id.gRecyclerView);
final GallaryRecyclerView adapter = new GallaryRecyclerView(context,fruitList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.context));
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getFruits().observe(this, new Observer<ArrayList<String>>() {
#Override
public void onChanged(ArrayList<String> strings) {
textView.setText((CharSequence) strings);
}
// #Override
// public void onChanged(#Nullable String s) {
// textView.setText(s);
// }
});
return root;
}
}
This is an GalleryRecyclerview:
public class GallaryRecyclerView extends RecyclerView.Adapter<GallaryRecyclerView.ViewHolder>{
private Context context;
private ArrayList<String> fruitList;
public GallaryRecyclerView(Context context, ArrayList<String> fruitList) {
this.context = context;
this.fruitList = fruitList;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView fruitsView;
ViewHolder(#NonNull View itemView) {
super(itemView);
fruitsView = itemView.findViewById(R.id.text_gallery);
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.fragment_gallery,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.fruitsView.setText(fruitList.get(position));
}
#Override
public int getItemCount() {
return fruitList.size();
}
}
You just have to instantiate fruitList inside onChanged method.
#Override
public void onChanged(ArrayList<String> strings) {
this.fruitList = strings;
}
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);
}
});
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();