一週的第一天,我有一個字符串數組持有平日的名字:設置使用數組
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;
}
我知道我可以做到這一點,即創建另一個字符串數組,但只是想知道是否有更有效的方法來設置使用相同字符串的一週的第一天。 – input 2012-08-17 17:58:40
如果是這樣的話,我會與@ BharatSinha的答案一起去。它將允許您控制基礎指數(0或1)並從那裏確定一週的其餘時間。請記住,雖然我發佈的方法同樣有效,因爲它仍然只分配一個數組。 – Eric 2012-08-17 18:00:16