【问题标题】:Set first day of the week using array使用数组设置一周的第一天
【发布时间】:2012-08-17 17:43:34
【问题描述】:

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

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

【问题讨论】:

    标签: java android date calendar


    【解决方案1】:

    您正试图从字符串中减去 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),并让您更清楚地了解返回的内容。

    【讨论】:

    • 我知道我可以做到这一点,即创建另一个字符串数组,但我只是想知道是否有更有效的方法来使用相同的字符串设置一周的第一天。
    • 如果是这样,我会选择@BharatSinha 的回答。它将允许您控制基本索引(0 或 1)并从那里确定一周的剩余时间。但请记住,我发布的方法同样有效,因为它仍然只分配一个数组。
    【解决方案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 并在经过一天后返回第二天和前一天等。

    【讨论】:

      【解决方案3】:

      在这种情况下我会做的是

      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
      

      编辑 要访问nth day,您应该使用...

      weekdays[n-1+days_start_index] 
      

      其他可以用这种方式调整:

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

      【讨论】:

      • 如果position == 0weekdays[position-1] 会抛出异常。 +1 为您的答案的第一部分 :)
      • @BharatSinha,我不确定为什么这不起作用。它应该按顺序显示工作日,但无论我选择星期日还是星期一,它都不会改变,它仍然显示星期一作为一周的第一天。
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        如果你想要 las 索引:

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

        【讨论】:

          【解决方案6】:

          你可以这样做:

          holder.txtWeekdays.setText(weekdays[position == 0? weekdays.length - 1 : position - 1]);
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

              我不太确定您的位置变量来自哪里,但按照您的思路,以下方法可行:

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

              我建议使用 Enum 来代替星期几。这将更具可读性。

              【讨论】:

              • 我已经更新了代码。请检查。这个抛出ArrayIndexOutofBoundsexception
              • 我现在明白了,您实际上需要整个数组,并且按正确的日期排列。我的错。
              猜你喜欢
              • 2014-08-28
              • 1970-01-01
              • 2019-08-30
              • 1970-01-01
              • 2012-08-22
              • 1970-01-01
              • 2020-01-18
              • 2012-11-22
              • 2019-10-07
              相关资源
              最近更新 更多