如何在 android 中使用 recyclerview 显示本地日期时间?

How to display local date type using recycle view in android?

我使用 Threeten 时区将本地日期存储在 LocalDate 类型的列表中。

这是我的代码:

private List<LocalDate> getWeekDays() {
        ZoneId z = ZoneId.of("Pacific/Auckland");  // Or ZoneId.of( "Africa/Tunis" )
        LocalDate today = LocalDate.now( z ) ;

        LocalDate localDate = today.with( org.threeten.bp.temporal.TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) ;
        List< LocalDate > dates = new ArrayList<>( 7 ) ;
        for( int i = 0 ; i < 7 ; i ++ ) {
            localDate = localDate.plusDays( i ) ;
            dates.add( localDate ) ;
        }
            return dates;

    }

问题出在将列表数组传递给回收视图之后。将其提取到回收视图时出现错误。

回收查看代码:

 public void onBindViewHolder(@NonNull HoldViews holder, int position) {

        holder.tx1.setText(WeekDays[position]);
        String[] date = Dates.toArray(new String[0]);// Dates is list array of type LocalDate
       holder.tx3.setText(date[position]);

    }

如果我转换成字符串数组。我收到以下错误“java.lang.ArrayStoreException:org.threeten.bp.LocPlDate 类型的源 [0] 无法存储在 java.lang.String[] 类型的目标数组中”。请帮助我。

与其每次绑定新视图时都将整个日期数组转换为字符串数组,不如直接使用给定的 position 直接拉取 LocalDate 对象。

然后使用 toString() 方法将 LocalDate 转换为字符串。

public void onBindViewHolder(@NonNull HoldViews holder, int position) {

    holder.tx1.setText(WeekDays[position]);
    String dateString = dateList.get(position).toString() // I don't know what the variable name you used is
    holder.tx3.setText(dateString);

}