gpt4 book ai didi

java - 如何使用android中的回收器 View 将事件放入日历中

转载 作者:行者123 更新时间:2023-12-05 00:02:40 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is a NullPointerException, and how do I fix it?

(12 个回答)


11 个月前关闭。




我正在使用 JAVA 创建一个 android 应用程序,其中将显示带有一些事件的日历。我在 GridLayoutManager 中的回收站 View 的帮助下创建日历并在 java.time.LocalDate 的帮助下解析日期
我需要帮助如何使用回收站 View 在网格布局单元格中显示事件。我不知道该怎么做以及如何将事件放在回收站 View 中。
我已经为适配器类、 View 持有者类、日历工具类和主要 Activity 类编写了代码。
适配器类:-


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

日历实用程序类
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;
}

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

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

}

我附上了两张图片:一张是我的输出,另一张是示例输出,即我想要实现的目标。
My Output
Sample Output
我已经厌倦了设置 Activity 日期,即 2021 年 8 月 15 日 onBindViewHolder作为:-
String date1 = "14/07/2021";
holder.event.setText("90% good"+date.format(DateTimeFormatter.ofPattern(date1)));
但出现错误: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference我真的需要帮助请...

最佳答案

您可以自定义 View 持有者布局以显示事件。
如果那天有任何事件,则显示它,否则,设置为事件 View 可见性。
为此,您可以将包含日期和事件的自定义类的 ArrayList 传递给适配器,而不是将 LocalDate 的 ArrayList 传递给适配器,例如 MyDate 的 ArrayList :

   class MyDate {
LocalDate date;
MyEvent event;
}

class MyEvent {
String title;
...
}
MyDate 类的 MyEvent 应该从您的数据库或任何其他方式填充。
并在您的 onbindview 持有人中使用它来显示事件:
    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);
}
}
}

关于java - 如何使用android中的回收器 View 将事件放入日历中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68715605/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com