Reputation: 7519
I have a string array which holds the names of the weekdays:
private final String[] weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
I have set Monday as the first day of the week by default.
Now I have given the user the option to select if s/he wants to set either Sunday or Monday as the first day of the week:
if (firstDay == 0) {
holder.txtWeekdays.setText(weekdays[position]);
}
Using the same String array how do I set Sunday as the first day of the Week?
else {
holder.txtWeekdays.setText(weekdays[position]-1); //this returns an error
}
UPDATED CODE:
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;
}
Upvotes: 0
Views: 3235
Reputation: 14363
What i would do in this case is
private final String[] weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
and
if (user selected monday) {
days_start_index = 1;
days_end_index = 7;
} else {
days_start_index = 0;
days_end_index = 6;
}
You can use it in the following manner...
for (int i=days_start_index; i<=days_end_index;i++)
// Do whatever you want
EDIT
To access n
th day you should use...
weekdays[n-1+days_start_index]
OR
Else could be tweaked in this manner:
if(position==0){
position = 7;
}
holder.txtWeekdays.setText(weekdays[position-1]);
Upvotes: 2
Reputation: 106
While it doesn't answer your question directly, this would be best implemented as an enum, something like this:
/*
* 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;
}
The values are then much more easily manipulated and even localized. You can write a method that takes a DayOfWeek and returns the next day and previous day when passed a day etc.
Upvotes: 2
Reputation: 49
I'm not really sure where your position variable comes from, but in your line of thinking, the following would work:
holder.txtWeekdays.setText(weekdays[position-1])
I would suggest using an Enum to hold the days of the week instead. This would be a bit more readable.
Upvotes: 0
Reputation: 1777
First of all I'll suggest to use enum for week days, since it's finite and predefined, plus you won't have ArrayIndexOutOfBoundsException. Secondly, I would create week abstraction, since it depends from context (e.g. usually working week is only 5 days, but in Israel it starts from Sunday), so this abstraction will have starting day, and collection of week days.
Upvotes: 0
Reputation: 117617
You can do it like this:
holder.txtWeekdays.setText(weekdays[position == 0? weekdays.length - 1 : position - 1]);
Upvotes: 0
Reputation: 67522
You are trying to subtract 1 from a string. That's not really going to work.
What you need to do is create a method that returns the day of the week. Something like this:
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];
}
Now, you COULD do this by position (using weekdays[position - 1]
), but the above will help you avoid errors in your code (indexing -1 for example), and give you more clarity as to what is being returned.
Upvotes: 2
Reputation: 1402
Maybe I'm wrong but is it something like this you want?
else {
holder.txtWeekdays.setText(weekdays[weekdays.length - 1]);
}
If you want the las index:
else {
holder.txtWeekdays.setText(weekdays[position - 1]);
}
Upvotes: 0
Reputation: 3639
it should be [position -1] instead of weekdays[position]-1 . and check for the position ==0 ( index error)
Upvotes: 0