3

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.

My Output

Sample Output

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...

Andrew
  • 123
  • 6

1 Answers1

0

you can customize the view holder layout to show events. if there is any event for that day, show it, otherwise, set gone for event views visibility.

to do this, instead of passing ArrayList of LocalDate to your adapter, you can pass ArrayList of Custom Class that has both date and event in it, for example, ArrayList of MyDate :

   class MyDate {
        LocalDate date;
        MyEvent event;
    }

    class MyEvent {
        String title;
        ...
    }

the MyEvent of MyDate class should fill from your database or any other way.

and in your onbindview holder use it to show events:

    ArrayList<MyDate> myDates; // instead of  ArrayList<LocalDate> daysOfMonth;

    public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {
        final MyDate myDate = myDates.get(position);
        if (myDate == null) {
            holder.dayOfMonth.setText("");
            holder.eventTextView.setText("");
        } else {

            // set date
            holder.dayOfMonth.setText(String.valueOf(myDate.date.getDayOfMonth()));

            //set background color
            if (myDate.date.equals(CalendarUtils.selectedDate)) {
                holder.parentView.setBackgroundColor(Color.BLUE);
            }

            //set event
            if (myDate.event != null) {
                holder.eventTextView.setText(myDate.event.title);
                holder.eventTextView.setVisibility(View.VISIBLE);
            } else {
                holder.eventTextView.setVisibility(View.GONE);
            }
        }
    }
  • So You mean I have to customize the `calendar_cell_hp` – Andrew Aug 13 '21 at 16:19
  • how do you get events? do you store them in DB or fetch from the internet?@Andrew – Iman Dolatkia Aug 14 '21 at 10:18
  • Right now I didn't have the events and neither I created any dataset nor model class for events. Yes later in future I will fetch the events from DB, but right now I want to show or I want to know how can I put events on particular/selected date? That means If selected date is 16th aug 2021 how can I put a text under that event? I hope you understand my question – Andrew Aug 14 '21 at 17:55
  • I update the answer @Andrew – Iman Dolatkia Aug 16 '21 at 07:35