Android-GregorianCalendar确定一天是否属于当前月份
我正在使用GridView和自定义BaseAdapter制作日历。我在函数中使用GregorianCalendar生成一个天数字符串数组,以便填充单元格。以下是处理日历布局的my Main活动:Android-GregorianCalendar确定一天是否属于当前月份,android,calendar,gregorian-calendar,Android,Calendar,Gregorian Calendar,我正在使用GridView和自定义BaseAdapter制作日历。我在函数中使用GregorianCalendar生成一个天数字符串数组,以便填充单元格。以下是处理日历布局的my Main活动: public class MainActivity extends Activity { private GridView calendarView, daysView; private final String[] weekdays = new String[]{"Mon", "Tu
public class MainActivity extends Activity {
private GridView calendarView, daysView;
private final String[] weekdays = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
daysView = (GridView) this.findViewById(R.id.daysGridView);
daysView.setAdapter(new WeekDayGridAdapter(MainActivity.this, weekdays));
calendarView = (GridView) this.findViewById(R.id.calendarGridView);
calendarView.setAdapter(new DayGridAdapter(MainActivity.this, getMonth()));
}
private String[] getMonth(){
int gridSizeX = 7, gridSizeY = 6;
int gridsize = gridSizeX * gridSizeY;
String[] myStringArray = new String[gridSizeX*gridSizeY];
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
GregorianCalendar gregorianCalendar = new GregorianCalendar(year, month, 1);
//this gets the day of week range 1-7, Monday - Sunday
int dayOfWeek = gregorianCalendar.get(Calendar.DAY_OF_WEEK);
//backtracks to the beginning of current week (Monday)
gregorianCalendar.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - dayOfWeek);
for (int i = 0; i < gridsize; i++)
{
myStringArray[i] = String.valueOf(gregorianCalendar.get(Calendar.DAY_OF_MONTH));
gregorianCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
return myStringArray;
}
}
公共类MainActivity扩展活动{
私有GridView日历视图,daysView;
私人最终字符串[]工作日=新字符串[]{“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
daysView=(GridView)this.findViewById(R.id.daysGridView);
setAdapter(新的WeekDayGridAdapter(MainActivity.this,weekdays));
calendarView=(GridView)this.findViewById(R.id.calendarGridView);
setAdapter(新的DayGridAdapter(MainActivity.this,getMonth());
}
私有字符串[]getMonth(){
int gridSizeX=7,gridSizeY=6;
int gridsize=gridSizeX*gridSizeY;
String[]myStringArray=新字符串[gridSizeX*gridSizeY];
最终日历=Calendar.getInstance();
int year=calendar.get(calendar.year);
int month=calendar.get(calendar.month);
GregorianCalendar GregorianCalendar=新的GregorianCalendar(年、月、1);
//这将得到星期日范围1-7,星期一到星期天
int dayOfWeek=gregorianCalendar.get(日历。一周中的一天);
//回溯到本周初(星期一)
gregorianalendar.add(Calendar.DAY\u OF年,Calendar.MONDAY-dayOfWeek);
对于(int i=0;i
下面是我的日历的外观:
我想要的是一种方法来确定哪些单元格属于相对于当前月份的上一个月或下一个月。对于上面的图像,该单元格将为:
- 前4名
- 最后9
我根据Jeroenvanevel的评论解决了这个问题 获取介于1(含)和1(不含)之间的每个日期 以下是我的自定义BaseAdapter:
public class DayGridAdapter extends BaseAdapter {
private Context context;
private final String[] days;
// global var to keep track of whether a day-cell belongs to the current month
Boolean in = false;
public DayGridAdapter(Context context, String[] days) {
this.context = context;
this.days = days;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.day_gridcell, null);
// coming from the previous month and encounter (for the first time) the cell "1"
if(days[position].equals("1") && in == false){
in = true;
}
// coming from the current month and encounter (for the second time) the cell "1"
else if (days[position].equals("1") && in == true){
in = false;
}
Button button = (Button) gridView.findViewById(R.id.day_grid_item);
button.setText(days[position]);
if(in){
//white background color
button.setBackgroundColor(context.getResources().getColor(R.color.white));
}else {
//light grey background color
button.setBackgroundColor(context.getResources().getColor(R.color.ligth2));
}
} else {
gridView = convertView;
}
return gridView;
}
@Override
public int getCount() {
return days.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
下面是它现在的样子:
您有当前月份,即
月份
。您知道如何获取日期的月份,即calendar.get(calendar.month)
。您只需比较以下两个:
if (calendar.get(Calendar.MONTH) == month) {
// Same month
} else {
// Not same month
}
编辑
这里的问题是,您在将它们提供给适配器之前预先计算了几天,同时丢失了一些信息。相反,在getView()中应该有类似的内容:
获取介于1(含)和1(不含)之间的每个日期。@Jeroenvanevel谢谢。我根据您的逻辑更改了BaseAdapter,它工作正常。我将发布代码。如果(calendar.get(calendar.MONTH)=MONTH{}您假定本周从周一开始。@njzk2谢谢,我将尝试您的建议。考虑到你对本周开始日的第二次评论,我知道本周开始于周一。我不是通过accindent完成的。不能保证项目视图是按顺序创建的。这将在小屏幕设备上失败。更不用说处理视图回收并不能处理任何事情。
public class DayGridAdapter extends BaseAdapter {
private Context context;
private final String[] days;
// global var to keep track of whether a day-cell belongs to the current month
Boolean in = false;
public DayGridAdapter(Context context, String[] days) {
this.context = context;
this.days = days;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.day_gridcell, null);
// coming from the previous month and encounter (for the first time) the cell "1"
if(days[position].equals("1") && in == false){
in = true;
}
// coming from the current month and encounter (for the second time) the cell "1"
else if (days[position].equals("1") && in == true){
in = false;
}
Button button = (Button) gridView.findViewById(R.id.day_grid_item);
button.setText(days[position]);
if(in){
//white background color
button.setBackgroundColor(context.getResources().getColor(R.color.white));
}else {
//light grey background color
button.setBackgroundColor(context.getResources().getColor(R.color.ligth2));
}
} else {
gridView = convertView;
}
return gridView;
}
@Override
public int getCount() {
return days.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
if (calendar.get(Calendar.MONTH) == month) {
// Same month
} else {
// Not same month
}
// Create a calendar for your month/year (these parameters you can pass to the adapter with little modifications)
GregorianCalendar gregorianCalendar = new GregorianCalendar(year, month, 1);
// Move back to the first day of the week (could be sunday)
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek());
// Move by *position*, being the index of the cell.
gregorianCalendar.add(Calendar.DAY_OF_MONTH, position);
String day = String.valueOf(gregorianCalendar.get(Calendar.DAY_OF_MONTH));
// Check the month for this particular day
if (calendar.get(Calendar.MONTH) == month) {
// Same month
} else {
// Not same month
}