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