如何使用android中的回收器视图将事件放入日历中 [英] How to put events in calendar using recycler view in android

查看:20
本文介绍了如何使用android中的回收器视图将事件放入日历中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JAVA 创建一个 android 应用程序,它将在其中显示包含一些事件的日历.我在 GridLayoutManager 中的回收器视图的帮助下创建日历,并在 java.time.LocalDate

的帮助下解析日期

我需要帮助 如何使用回收器视图在网格布局单元格中显示事件.我不知道如何做到这一点,也不知道如何将事件置于回收者视图中.我已经为日历和主要活动类编写了适配器类、viewholder 类、Calendar utils 类的代码.

适配器类:-

<预><代码>私有最终 ArrayListdaysOfMonth;私有最终 OnItemListner onItemListner;公共 CalAdapter(ArrayList daysOfMonth, OnItemListner onItemListner) {this.daysOfMonth = daysOfMonth;this.onItemListner = onItemListner;}public CalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {LayoutInflater inflater = LayoutInflater.from(parent.getContext());查看视图 = inflater.inflate(R.layout.calendar_cell_hp, parent, false);ViewGroup.LayoutParams layoutParams = view.getLayoutParams();layoutParams.height = (int) (parent.getHeight() * 0.166666666);//这将给日历单元格高度返回新的 CalViewHolder(view, onItemListner);}public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {//holder.dayOfMonth.setText(daysOfMonth.get(position));最终 LocalDate 日期 = daysOfMonth.get(position);如果(日期==空)holder.dayOfMonth.setText("");别的 {holder.dayOfMonth.setText(String.valueOf(date.getDayOfMonth()));if (date.equals(CalendarUtils.selectedDate))//这将显示当前日期holder.parentView.setBackgroundColor(Color.BLUE);//这将显示当前日期}}公共 int getItemCount() {返回 daysOfMonth.size();}//这将显示点击日期公共接口 OnItemListner{void onItemClick(int position, String dayText);}

ViewHolder 类

<预><代码>类 CalViewHolder 扩展 RecyclerView.ViewHolder 实现 View.OnClickListener {公共最终视图父视图;公共最终 TextView dayOfMonth;私有最终 CalAdapter.OnItemListner onItemListner;public CalViewHolder(@NonNull View itemView, CalAdapter.OnItemListner onItemListner) {超级(项目视图);parentView = itemView.findViewById(R.id.parentView);dayOfMonth = itemView.findViewById(R.id.cellDayTextHP);this.onItemListner = onItemListner;itemView.setOnClickListener(this);}@覆盖public void onClick(View v) {onItemListner.onItemClick(getAdapterPosition(), (String) dayOfMonth.getText());}}

日历实用工具类

public static LocalDate selectedDate;public static String monthYearFromDate(LocalDate date){DateTimeFormatter formatter = DateTimeFormatter.ofPattern(MMMM yyyy");返回日期格式(格式化程序);}@RequiresApi(api = Build.VERSION_CODES.O)公共静态 ArrayListdaysInMonthArray(LocalDate 日期){ArrayListdaysInMonthArray = 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);别的daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek));}返回 daysInMonthArray;}

这里我使用方法在MainActivity.java中设置月视图:

<预><代码>私有无效 setMonthView() {monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));ArrayListdaysInMonth = daysInMonthArray(CalendarUtils.selectedDate);CalAdapter calAdapter = new CalAdapter(daysInMonth, this);RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);calendarRecyclerView.setLayoutManager(layoutManager);calendarRecyclerView.setAdapter(calAdapter);}

我附上了两张图片:一张是我的输出,另一张是示例输出,即我想要实现的目标.

我已经厌倦了将 事件 设置为日期,即 onBindViewHolder 上的 15/08/2021 为:-

String date1 = "14/07/2021";holder.event.setText(90% 好"+date.format(DateTimeFormatter.ofPattern(date1)));

但出现错误:尝试在空对象引用上调用虚方法'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)'

我真的需要帮助...

解决方案

您可以自定义视图支架布局以显示事件.如果当天有任何事件,则显示它,否则,将事件视图设置为消失.

要做到这一点,您可以传递包含日期和事件的自定义类的 ArrayList,而不是将 LocalDate 的 ArrayList 传递给您的适配器,例如 MyDate 的 ArrayList :

 class MyDate {本地日期;MyEvent 事件;}类我的事件{字符串标题;...}

MyDate 类的 MyEvent 应该从您的数据库或任何其他方式填充.

并在您的 onbindview 持有者中使用它来显示事件:

 ArrayList我的日期;//而不是 ArrayListdaysOfMonth;public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {final MyDate myDate = myDates.get(position);如果(我的日期 == 空){holder.dayOfMonth.setText("");holder.eventTextView.setText("");} 别的 {//设置日期holder.dayOfMonth.setText(String.valueOf(myDate.date.getDayOfMonth()));//设置背景颜色如果(myDate.date.equals(CalendarUtils.selectedDate)){holder.parentView.setBackgroundColor(Color.BLUE);}//设置事件如果(myDate.event != null){holder.eventTextView.setText(myDate.event.title);holder.eventTextView.setVisibility(View.VISIBLE);} 别的 {holder.eventTextView.setVisibility(View.GONE);}}}

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

解决方案

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

这篇关于如何使用android中的回收器视图将事件放入日历中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆