0

我有一个字符串数组,其中包含工作日的名称:

private final String[] weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

默认情况下,我已将星期一设置为一周的第一天。

现在,我为用户提供了选择是否要将星期日或星期一设置为一周的第一天的选项:

if (firstDay == 0) {
    holder.txtWeekdays.setText(weekdays[position]);
 }

使用相同的字符串数组如何将星期日设置为一周的第一天?

else {
     holder.txtWeekdays.setText(weekdays[position]-1);  //this returns an error 
}

更新代码:

public class CalendarWeekAdapter extends BaseAdapter{

private final String[] weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
Context mContext;

   private LayoutInflater mInflater;
   public CalendarWeekAdapter(Context c)
   {
          mContext=c;
          mInflater = LayoutInflater.from(c);
   }
   public int getCount()
   {
          return weekdays.length;
   }
   public Object getItem(int position)
   {
          return position;
   }
   public long getItemId(int position)
   {
          return position;
   }
   public View getView(int position, View convertView, ViewGroup parent)
   {
          ViewHolder holder=null;
          if(convertView==null)
          {
                 convertView = mInflater.inflate(R.layout.calendar_week_gridcell, parent,false);
                 holder = new ViewHolder();
                 holder.txtWeekdays=(TextView)convertView.findViewById(R.id.weekdays);
                 if(position==0)
                 {                             
                       convertView.setTag(holder);
                 }
          }
          else
          {
                 holder = (ViewHolder) convertView.getTag();
          }

          if (firstDay == 0) {
              holder.txtWeekdays.setText(weekdays[position]);
          }
          else {
              holder.txtWeekdays.setText(weekdays[position]); 
          }

          return convertView;
   }

}
static class ViewHolder
{        
      TextView txtWeekdays;                 
}
4

8 回答 8

2

虽然它不能直接回答您的问题,但最好将其实现为枚举,如下所示:

/*
 * Easily Localize these by setting the short and long day
 * names using standard Java localization techniques
 * 
 * */
enum DayOfWeek {
    MONDAY("Mon", "Monday"), 
    TUESDAY("Tue", "Tuesday"),
    WEDNESAY("Wed", "Wednesday"),
    THURSDAY("Thurs", "Thursday"),
    FRIDAY("Fri", "Friday"),
    SATURDAY("Sat", "Saturday"),
    SUNDAY("Sun", "Sunday");


    DayOfWeek(final String shortDayName, final String longDayName) {
         this.shortName = shortDayName;
         this.longName = longDayName;
    }

    public String shortName() {
        return shortName;
    }

    public String longName() {
        return longName;
    }

    private final String shortName;
    private final String longName;
}

然后这些值更容易被操纵甚至本地化。您可以编写一个方法,该方法采用 DayOfWeek 并在经过一天后返回第二天和前一天等。

于 2012-08-17T18:08:20.227 回答
2

您正在尝试从字符串中减去 1。那是行不通的。

您需要做的是创建一个返回星期几的方法。像这样的东西:

public String getDayOfWeek(int day, int firstDay) {
   String[] weekdays;
   if (firstDay == 0)
      weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
   else
      weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
   return weekdays[day];
}

现在,您可以按位置(使用weekdays[position - 1])执行此操作,但以上内容将帮助您避免代码中的错误(例如索引 -1),并让您更清楚地了解返回的内容。

于 2012-08-17T17:48:31.623 回答
2

在这种情况下我会做的是

private final String[] weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

if (user selected monday) {
    days_start_index = 1;
    days_end_index = 7;
} else {
    days_start_index = 0;
    days_end_index = 6;
}

您可以通过以下方式使用它...

for (int i=days_start_index; i<=days_end_index;i++)
    // Do whatever you want

编辑 要访问n一天,您应该使用...

weekdays[n-1+days_start_index] 

或者

否则可以通过这种方式进行调整:

if(position==0){
    position = 7;
}
holder.txtWeekdays.setText(weekdays[position-1]);
于 2012-08-17T17:50:02.893 回答
0

你可以这样做:

holder.txtWeekdays.setText(weekdays[position == 0? weekdays.length - 1 : position - 1]);
于 2012-08-17T17:50:34.363 回答
0

我不太确定你的位置变量来自哪里,但在你的思路中,以下方法会起作用:

holder.txtWeekdays.setText(weekdays[position-1])

我建议使用枚举来代替一周中的日子。这将更具可读性。

于 2012-08-17T17:50:50.833 回答
0

首先,我建议在工作日使用枚举,因为它是有限且预定义的,而且您不会有 ArrayIndexOutOfBoundsException。其次,我会创建周抽象,因为它取决于上下文(例如,通常工作周只有 5 天,但在以色列它从星期日开始),所以这个抽象将有开始日和工作日的集合。

于 2012-08-17T17:56:18.607 回答
0

它应该是 [position -1] 而不是weekdays[position]-1。并检查位置 ==0 (索引错误)

于 2012-08-17T17:48:15.767 回答
0

也许我错了,但你想要这样的东西吗?

else {
     holder.txtWeekdays.setText(weekdays[weekdays.length - 1]); 
}

如果你想要 las 索引:

else {
     holder.txtWeekdays.setText(weekdays[position - 1]); 
}
于 2012-08-17T17:48:26.943 回答