'Problem with Gregorian Calendar, not showing all of the days of the month
I have a problem with my Gregorian calendar which I have added into my gridView from adapter. For some of the months only a certain number of days appear. I have no idea why it does this,
Calendar.xml
<GridView
android:id="@+id/gv_calendar"
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:cacheColorHint="#ffffff"
android:gravity="center"
android:columnWidth="1dp"
android:minHeight="4dp"
android:scrollbars="none"
android:listSelector="@color/transparent"
android:numColumns="7"
android:verticalSpacing="0dp"
android:padding="0dp"
android:stretchMode="columnWidth"
android:textAlignment="gravity">
</GridView>
Calendar.java
Code for calendar(gridview)
( new HomeCollection("2022-03-05" ,"Christmas","Holiday","this is holiday"));
cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
cal_month_copy = (GregorianCalendar) cal_month.clone();
hwAdapter = new HwAdapter(this, cal_month,HomeCollection.date_collection_arr);
tv_month = (TextView) findViewById(R.id.tv_month);
tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
ImageButton previous = (ImageButton) findViewById(R.id.ib_prev);
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cal_month.get(GregorianCalendar.MONTH) == 4&&cal_month.get(GregorianCalendar.YEAR)==2021) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) - 1), cal_month.getActualMaximum(GregorianCalendar.MONTH), 1);
Toast.makeText(Activity10.this, "Event Detail is available for current session only.", Toast.LENGTH_SHORT).show();
}
else {
setPreviousMonth();
refreshCalendar();
}
}
});
ImageButton next = (ImageButton) findViewById(R.id.Ib_next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cal_month.get(GregorianCalendar.MONTH) == 5&&cal_month.get(GregorianCalendar.YEAR)==2026) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) + 1), cal_month.getActualMinimum(GregorianCalendar.MONTH), 1);
Toast.makeText(Activity10.this, "Event Detail is available for current session only.", Toast.LENGTH_SHORT).show();
}
else {
setNextMonth();
refreshCalendar();
}
}
});
GridView gridview = (GridView) findViewById(R.id.gv_calendar);
gridview.setAdapter(hwAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String selectedGridDate = HwAdapter.day_string.get(position);
}
});
protected void setNextMonth() {
if (cal_month.get(GregorianCalendar.MONTH) == cal_month.getActualMaximum(GregorianCalendar.MONTH)) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) + 1), cal_month.getActualMinimum(GregorianCalendar.MONTH), 1);
} else {
cal_month.set(GregorianCalendar.MONTH,
cal_month.get(GregorianCalendar.MONTH) + 1);
}
}
protected void setPreviousMonth() {
if (cal_month.get(GregorianCalendar.MONTH) == cal_month.getActualMinimum(GregorianCalendar.MONTH)) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) - 1), cal_month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
cal_month.set(GregorianCalendar.MONTH, cal_month.get(GregorianCalendar.MONTH) - 1);
}
}
public void refreshCalendar() {
hwAdapter.refreshDays();
hwAdapter.notifyDataSetChanged();
tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
}
HwAdapter.java
public HwAdapter(Activity context, GregorianCalendar monthCalendar, ArrayList<HomeCollection> date_collection_arr) {
this.date_collection_arr = date_collection_arr;
HwAdapter.day_string = new ArrayList<String>();
Locale.setDefault(Locale.US);
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
this.context = context;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.cal_item1, null);
}
dayView = (TextView) v.findViewById(R.id.date);
String[] separatedTime = day_string.get(position).split("-");
gridvalue = separatedTime[2].replaceFirst("^0*", "");
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
dayView.setTextColor(Color.parseColor("#A9A9A9"));
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.parseColor("#A9A9A9"));
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting current month's days in white color.
dayView.setTextColor(Color.parseColor("#ffffff"));
}
if (day_string.get(position).equals(curentDateString)) {
dayView.setTextColor(Color.parseColor("#F29027"));
} else {
v.setBackgroundColor(Color.parseColor("#005D7575"));
}
dayView.setText(gridvalue);
// create date string for comparison
String date = day_string.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
setEventView(v, position, dayView);
return v;
}
public void refreshDays() {
// clear items
items.clear();
day_string.clear();
Locale.setDefault(Locale.US);
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
pmonthmaxset = (GregorianCalendar) pmonth.clone();
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
day_string.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
public void setEventView(View v, int pos, TextView txt) {
int len = HomeCollection.date_collection_arr.size();
for (int i = 0; i < len; i++) {
HomeCollection cal_obj = HomeCollection.date_collection_arr.get(i);
String date = cal_obj.date;
int len1 = day_string.size();
if (len1 > pos) {
if (day_string.get(pos).equals(date)) {
if ((Integer.parseInt(gridvalue) > 1) && (pos < firstDay)) {
} else if ((Integer.parseInt(gridvalue) < 7) && (pos > 28)) {
} else {
v.setBackgroundColor(Color.parseColor("#005D7575"));
v.setBackgroundResource(R.drawable.rounded_calender);
txt.setTextColor(Color.parseColor("#2F8FD6"));
}
}
}
}
}
public void getPositionList(String date, final Activity act) {
int len = HomeCollection.date_collection_arr.size();
JSONArray jbarrays = new JSONArray();
for (int j = 0; j < len; j++) {
if (HomeCollection.date_collection_arr.get(j).date.equals(date)) {
HashMap<String, String> maplist = new HashMap<String, String>();
maplist.put("hnames", HomeCollection.date_collection_arr.get(j).name);
maplist.put("hsubject", HomeCollection.date_collection_arr.get(j).subject);
maplist.put("descript", HomeCollection.date_collection_arr.get(j).description);
JSONObject json1 = new JSONObject(maplist);
jbarrays.put(json1);
}
}
if (jbarrays.length() != 0) {
final Dialog dialogs = new Dialog(context);
dialogs.setContentView(R.layout.dialog_inform);
dialogs.show();
}
}
private ArrayList<Dialogpojo> getMatchList(String detail) {
try {
JSONArray jsonArray = new JSONArray(detail);
alCustom = new ArrayList<Dialogpojo>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
Dialogpojo pojo = new Dialogpojo();
pojo.setTitles(jsonObject.optString("hnames"));
pojo.setSubjects(jsonObject.optString("hsubject"));
pojo.setDescripts(jsonObject.optString("descript"));
alCustom.add(pojo);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
