2012-08-17 33 views
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;     
} 

回答

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爲例)的錯誤,給你更清晰,以什麼是返回。

+0

我知道我可以做到這一點,即創建另一個字符串數組,但只是想知道是否有更有效的方法來設置使用相同字符串的一週的第一天。 – input 2012-08-17 17:58:40

+1

如果是這樣的話,我會與@ BharatSinha的答案一起去。它將允許您控制基礎指數(0或1)並從那裏確定一週的其餘時間。請記住,雖然我發佈的方法同樣有效,因爲它仍然只分配一個數組。 – Eric 2012-08-17 18:00:16

0

它應該是[-1位]代替平日[位置] -1-。並檢查位置== 0(索引錯誤)

+0

對不起,請檢查更新的代碼。 – input 2012-08-17 17:57:37

0

也許我錯了,但它是這樣的東西你想要的?

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

如果你想在拉斯維加斯指數:

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

我更新了代碼。 – input 2012-08-17 18:00:51

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] 

OR

人可以以這種方式進行調整:

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

'weekdays [position-1]'如果位置== 0會拋出異常。 +1爲您的答案的第一部分:) – 2012-08-17 18:01:40

+0

@BharatSinha,我不知道爲什麼這不起作用。它應該顯示訂單中的工作日,但不會預測是選擇星期日還是星期一,但它仍將星期一顯示爲一週的第一天。 – input 2012-08-17 18:10:02

0

你可以這樣說:

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

我真的不知道在哪裏你的位置變量來自於,但根據你的思路,下面的問題會出現k:

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

我會建議使用枚舉來保存一週的日子,而不是。這會更可讀。

+0

編輯,以更好地縮小我的答案增加一個枚舉 – 2012-08-17 17:58:03

+0

我已經更新了代碼。請檢查。這一個拋出'ArrayIndexOutofBoundsexception' – input 2012-08-17 17:59:41

+0

我現在看到,你實際上需要整個數組,以適當的順序的日子。我的錯。 – 2012-08-17 18:11:11

0

首先,我會建議使用枚舉爲一週,因爲它是有限的和預定義的,再加上你不會有ArrayIndexOutOfBoundsException。其次,我會創建一週抽象,因爲它取決於上下文(例如,通常工作周只有5天,但在以色列它從星期天開始),所以這個抽象將具有開始日和收集週日。

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並返回第二天和前一天,當經過一天等。