i am trying to add a recyclerview on a fragment
fragments code:
public class MediaPlayerController extends Fragment{
private static final String TAG = "MediaPlayerController";
private RecyclerView recyclerViewb;
private RecycleViewAdapter myAdapter;
private ArrayList<String> myRecordings = new ArrayList<>();
//private DBHelper dbHelper;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: started");
View v = inflater.inflate(R.layout.media_player_area, container, false);
recyclerViewb = (RecyclerView)v.findViewById(R.id.recyclerViewXml);
//dbHelper = new DBHelper(v.getContext(), null, null, 1);
myRecordings.add("hau");
myRecordings.add("hau2");
myRecordings.add("hau3");
myRecordings.add("hau4");
myRecordings.add("ha5");
myRecordings.add("hau23");
myRecordings.add("ha31u");
myRecordings.add("haudsa");
System.out.println("what does this print?"+myRecordings);
myAdapter = new RecycleViewAdapter(v.getContext(), myRecordings);
recyclerViewb.setAdapter(myAdapter);
recyclerViewb.setLayoutManager(new LinearLayoutManager(v.getContext()));
Log.d(TAG, "onCreateView: returned");
return v;
}
in the logs i see that that it doesn't even get to the RecycleViewAdapter sections of code at all the fragment is completely blank
i get not errors as well
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private static final String TAG = "RecycleViewAdapter";
private Context mContext;
private ArrayList<String> mRecName = new ArrayList<>();
public RecycleViewAdapter(Context mContext, ArrayList<String> mRecName) {
this.mContext = mContext;
this.mRecName = mRecName;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: i am here");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_recycle, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: called");
holder.recTxt.setText(mRecName.get(position).toString());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "sas gamaw", Toast.LENGTH_LONG);
}
});
}
#Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
RelativeLayout parentLayout;
TextView recTxt;
public ViewHolder(View itemView) {
super(itemView);
recTxt = itemView.findViewById(R.id.recTextView);
parentLayout = itemView.findViewById(R.id.parentLayoutXml);
}
}
}
in the logs it only displays oncreateView started and the myRecordings arraylist
onCreateView: started
I/System.out: what does this print?[hau, hau2, hau3, hau4, ha5,
hau23, ha31u, haudsa]
MediaPlayerController: onCreateView: returned
i can't think of what is going wrong :/ please help out
i also want to add that i am fairly new to android studio thanks
your code problem it is in Recycler Adapter in getItemCount() method,
You don't return any Item , you must change it to :
#Override
public int getItemCount() {
return mRecName.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
i am trying to create a bottom sheet fragment with a recycler view. But i am getting an error in recycler view as adapter not set skipping layout
I am positive that i set the adapter for recycler view.I do not seem to understand the problem.
Bottomsheet Fragment code
public class QueueBottomSheetDialog extends BottomSheetDialogFragment {
//Views
private View fragmentView;
private RecyclerView queueRecyclerView;
private QueueRecyclerAdapter queueRecyclerAdapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.queue_bottomsheet,container,false);
Initialize();
populateSongs();
return fragmentView;
}
private void Initialize(){
queueRecyclerView = fragmentView.findViewById(R.id.queue_recyclerView);
queueRecyclerView.setHasFixedSize(true);
}
private void populateSongs(){
queueRecyclerAdapter = new QueueRecyclerAdapter(getContext(),MusicService.songsSet);
queueRecyclerView.setAdapter(queueRecyclerAdapter);
queueRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
Code for recycler adapter
public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdapter.QueueViewHolder> {
private Context context;
private SongSet songSet;
public QueueRecyclerAdapter(Context context,SongSet songSet){
this.context = context;
this.songSet = songSet;
}
#NonNull
#Override
public QueueViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.queue_cardview,parent,false);
return new QueueViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull QueueViewHolder holder, int position) {
holder.songTv.setText(songSet.get(position).getTitle());
holder.artistTv.setText(songSet.get(position).getArtist());
}
#Override
public int getItemCount() {
return songSet.size();
}
public static class QueueViewHolder extends RecyclerView.ViewHolder{
private TextView songTv;
private TextView artistTv;
public QueueViewHolder(#NonNull View itemView) {
super(itemView);
songTv = itemView.findViewById(R.id.sName);
artistTv = itemView.findViewById(R.id.aName);
}
}
}
I am calling like this in my main activity
queueBottomSheetDialog queueBottomSheetDialog = newQueueBottomSheetDialog();
queueBottomSheetDialog.show(getSupportFragmentManager(),"QUEUE BOTTOMSHEET");
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 want to grey out fragment activity on some condition, I am trying to set a flag in pojo class and checking every time i try to grey out an activity.
I do not know where exactly the logic should be placed. I tried to put it inside a onCreateView() method, but it gave me an null pointer exception.
Please find the code below for your reference.
#SuppressLint("ValidFragment")
public class QuizTab extends Fragment {
private static final String TAG = "QuizTab";
RecyclerView recyclerView;
View view;
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.quiztab, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
if(flag){
grey out logic ---- not working
}
getQuizContent();
setupRecyclerView(recyclerView);
return view;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity()));
}
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public final LinearLayout mLinearlayout;
public ViewHolder(View view) {
super(view);
mLinearlayout = (LinearLayout) view.findViewById(R.id.mcq);
}
}
public SimpleStringRecyclerViewAdapter(Context context) {
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quiztab_mcq, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
}
#Override
public int getItemCount() {
return 1;
}
}
}
LinearLayout in this case is parent of fragment i tryed to paint fragment but failure so i did something like that
if(flag){
LinearLayout layout = view.findViewById(R.id.parentOffragment);
layout.setBackgroundColor(Color.GRAY);
}
I am trying to pull data from Parse.com cloud to my android application.This is the code for my custom adapter:
public class AdvertisingAdapter extends RecyclerView.Adapter<AdvertisingAdapter.TatsViewHolder> {
List<AdvertInfo> data = new ArrayList<>();
private LayoutInflater inflater;
public AdvertisingAdapter(Context context, List<AdvertInfo> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public TatsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.single_ad, parent, false);
TatsViewHolder holder = new TatsViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(TatsViewHolder holder, int position) {
AdvertInfo current = data.get(position);
holder.daPoster.setText(current.theadvertiser);
holder.daAdvert.setText(current.theadvert);
}
#Override
public int getItemCount() {
return data.size();
}
class TatsViewHolder extends RecyclerView.ViewHolder{
TextView daPoster;
TextView daAdvert;
public TatsViewHolder(View itemView) {
super(itemView);
daPoster = (TextView)itemView.findViewById(R.id.txtAdvertiser);
daAdvert = (TextView)itemView.findViewById(R.id.txtAdvert);
}
}
}
and the code for my member class
public class AdvertInfo {
public String theadvertiser;
public String theadvert;
}
then for my fragment where i am displaying the data here is the code
public class ViewAdverts extends android.support.v4.app.Fragment {
private RecyclerView myRecyclerView;
AdvertisingAdapter myAdapter;
public ViewAdverts() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.view_adverts, container, false);
myRecyclerView = (RecyclerView)layout.findViewById(R.id.advert_list);
myAdapter = new AdvertisingAdapter(getActivity(), getData());
myRecyclerView.setAdapter(myAdapter);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<AdvertInfo> getData(){
final List<AdvertInfo> myposts = new ArrayList<>();
ParseQuery<Posts> query = ParseQuery.getQuery(Posts.class);
query.findInBackground(new FindCallback<Posts>() {
#Override
public void done(List<Posts> list, ParseException e) {
if (e == null){
for (int i = 0; i < list.size(); i++){
AdvertInfo ad = new AdvertInfo();
ad.theadvertiser = list.get(i).getString(ParseConstants.ADVERTISER);
ad.theadvert = list.get(i).getString(ParseConstants.DESCRIPTION);
myposts.add(ad);
}
}
}
});
return myposts;
}
}
I have tried to debug by putting breaks on getData method in the fragment I am get the data but I dont know why my recycler view appearing empty when I run.