我有一个字符串数组,其中包含工作日的名称:
private final String[] weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
默认情况下,我将星期一定为一周的第一天。
现在我已经让用户选择是否要将星期日或星期一设置为一周的第一天:
if (firstDay == 0) {
holder.txtWeekdays.setText(weekdays[position]);
}
使用相同的String数组,如何将星期日设置为本周的第一天?
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;
}
答案 0 :(得分: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),并让您更清楚地了解返回的内容
答案 1 :(得分: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]);
答案 2 :(得分: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 :(得分:0)
它应该是[位置-1]而不是工作日[位置] -1 。并检查位置== 0(索引错误)
答案 4 :(得分:0)
也许我错了,但这是你想要的吗?
else {
holder.txtWeekdays.setText(weekdays[weekdays.length - 1]);
}
如果你想要las索引:
else {
holder.txtWeekdays.setText(weekdays[position - 1]);
}
答案 5 :(得分:0)
你可以这样做:
holder.txtWeekdays.setText(weekdays[position == 0? weekdays.length - 1 : position - 1]);
答案 6 :(得分:0)
我不确定你的职位变量来自哪里,但在你的思路中,以下方法可行:
holder.txtWeekdays.setText(weekdays[position-1])
我建议使用Enum代替一周的日子。这会更具可读性。
答案 7 :(得分:0)
首先,我建议在工作日使用枚举,因为它是有限的和预定义的,加上你不会有ArrayIndexOutOfBoundsException。其次,我会创建周抽象,因为它取决于上下文(例如,通常工作周只有5天,但在以色列从星期日开始),所以这个抽象将有开始日和星期集合。