I am creating an android app using JAVA, in which it will display a calendar with some events. I create the calendar with the help of recycler view in GridLayoutManager and parsing the date with the help of java.time.LocalDate
I need help How to display the events in the grid layout cell using recycler view. I don't know how to do that and how to put the events in the recycler view. I have write the code for adapter class, viewholder class, Calendar utils class for calendar and main activity class.
Adapter class:-
private final ArrayList<LocalDate> daysOfMonth;
private final OnItemListner onItemListner;
public CalAdapter(ArrayList<LocalDate> daysOfMonth, OnItemListner onItemListner) {
this.daysOfMonth = daysOfMonth;
this.onItemListner = onItemListner;
}
public CalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.calendar_cell_hp, parent, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = (int) (parent.getHeight() * 0.166666666); //This will give the calendar cell hieght
return new CalViewHolder(view, onItemListner);
}
public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {
//holder.dayOfMonth.setText(daysOfMonth.get(position));
final LocalDate date = daysOfMonth.get(position);
if (date == null)
holder.dayOfMonth.setText("");
else {
holder.dayOfMonth.setText(String.valueOf(date.getDayOfMonth()));
if (date.equals(CalendarUtils.selectedDate)) //this will display current date
holder.parentView.setBackgroundColor(Color.BLUE); //this will display current date
}
}
public int getItemCount() {
return daysOfMonth.size();
}
//this will show the date on click
public interface OnItemListner{
void onItemClick(int position, String dayText);
}
ViewHolder class
class CalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final View parentView;
public final TextView dayOfMonth;
private final CalAdapter.OnItemListner onItemListner;
public CalViewHolder(@NonNull View itemView, CalAdapter.OnItemListner onItemListner) {
super(itemView);
parentView = itemView.findViewById(R.id.parentView);
dayOfMonth = itemView.findViewById(R.id.cellDayTextHP);
this.onItemListner = onItemListner;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onItemListner.onItemClick(getAdapterPosition(), (String) dayOfMonth.getText());
}
}
Calendar Utils class
public static LocalDate selectedDate;
public static String monthYearFromDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
return date.format(formatter);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static ArrayList<LocalDate> daysInMonthArray(LocalDate date)
{
ArrayList<LocalDate> daysInMonthArray = new ArrayList<>();
YearMonth yearMonth = YearMonth.from(date);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate firstOfMonth = CalendarUtils.selectedDate.withDayOfMonth(1);
int dayOfWeek = firstOfMonth.getDayOfWeek().getValue();
for(int i = 1; i <= 42; i++)
{
if(i <= dayOfWeek || i > daysInMonth + dayOfWeek)
daysInMonthArray.add(null);
else
daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek));
}
return daysInMonthArray;
}
Here I have use method to set the month view in MainActivity.java:
private void setMonthView() {
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> daysInMonth = daysInMonthArray(CalendarUtils.selectedDate);
CalAdapter calAdapter = new CalAdapter(daysInMonth, this);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calAdapter);
}
I have attached two images: One is my output and another one sample output, i.e., what I want to achieved.
I have tired to set the Event on date ie., 15/08/2021 on onBindViewHolder as:-
String date1 = "14/07/2021";
holder.event.setText("90% good"+date.format(DateTimeFormatter.ofPattern(date1)));
but got error as:
Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference
I really need help please...