我正在创建包含事件的日历,我想在特定日期显示事件总数。我在用 CalendarAdapter.java类。显示总数没有。事件计数(如通知气泡)我正在使用的日期 Badge.java类。现在问题是徽章适用于当前月份首先显示的日历日期, 更改月份后,该徽章不会从下个月的日历中删除。
日历适配器:
package vyClean.wemecalendar.adapters;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import vyClean.wemecalendar.activity.Event1;
import vyClean.wemecalendar.util.BadgeView;
import vyClean.wemecalendar.util.BadgeView1;
import vyClean.wemecalendar.util.CalendarEvent;
import vyClean.wemecalendar.R;
import vyClean.wemecalendar.util.ToastMessage;
public class CalendarAdapter extends BaseAdapter {
private Context context;
private java.util.Calendar month;
public GregorianCalendar pmonth;
/*-----------calendar instance for previous month for getting complete view-------------------*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
public static TextView textno;
RelativeLayout cal_item_ll;
private ArrayList<String> items;
public static List<String> day_string;
private View previousView;
private View getView;
public ArrayList<CalendarEvent> event_calendar_arr;
public CalendarAdapter(Context context, GregorianCalendar monthCalendar, ArrayList<CalendarEvent> event_calendar_arr) {
this.event_calendar_arr = event_calendar_arr;
CalendarAdapter.day_string = new ArrayList<>();
Locale.setDefault(Locale.getDefault());
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
this.context = context;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public int getCount() {
return day_string.size();
}
public Object getItem(int position) {
return day_string.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
getView = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getView = vi.inflate(R.layout.cal_item, parent, false);
cal_item_ll = (RelativeLayout) getView.findViewById(R.id.cal_item_ll);
//textno = (TextView) getView.findViewById(R.id.textno);
}
dayView = (TextView) getView.findViewById(R.id.date);
String[] separatedTime = day_string.get(position).split("-");
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
dayView.setTextColor(Color.GRAY);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.GRAY);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.parseColor("#ff69b4"));
}
if (day_string.get(position).equals(curentDateString)) {
getView.setBackgroundColor(Color.CYAN);
} else {
getView.setBackgroundColor(Color.WHITE);
}
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;
}
int len = CalendarEvent.event_calendar_arr.size();
HashSet<String> eventListSet1 = new HashSet<>();
int flag = 0;
for (int j = 0; j < len; j++) {
//textno.setTextColor(Color.WHITE);
CalendarEvent cal_event = CalendarEvent.event_calendar_arr.get(j);
if (day_string.get(position).equals(cal_event.getEventFromDate())) {
eventListSet1.add(cal_event.getEventCateId());
flag = 1;
}
}
if (flag == 1) {
setEventView(getView, position, dayView, eventListSet1);
/*----------call badgeview here--------*/
/*----------call badgeview on linear layout (cal_item_ll) which is inside getview--------*/
BadgeView badge = new BadgeView(context, cal_item_ll);
badge.setText("1");
badge.show();
}
System.out.println("eventListSet1:" + eventListSet1);
return getView;
}
public View setSelected(View view, int pos) {
if (previousView != null) {
// previousView.setBackgroundColor(Color.CYAN);
previousView.setBackgroundColor(Color.WHITE);
}
view = previousView;
// view.setBackgroundColor(Color.CYAN);
int len = day_string.size();
if (len > pos) {
if (day_string.get(pos).equals(curentDateString)) {
} else {
previousView = view;
}
}
return view;
}
public void refreshDays() {
// clear items
items.clear();
day_string.clear();
Locale.setDefault(Locale.getDefault());
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 ...
//Calendar instance for getting a complete gridview including the three month's (previous,current,next) dates.
pmonthmaxset = (GregorianCalendar) pmonth.clone();
//setting the start date as previous month's required date.
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
// filling calendar gridview.
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
System.out.println(" 214 itemvale :" + itemvalue);
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, HashSet<String> eventListSet1) {
ArrayList<String> eventList = new ArrayList<>();
int len = CalendarEvent.event_calendar_arr.size();
for (int i = 0; i < len; i++) {
CalendarEvent cal_event = CalendarEvent.event_calendar_arr.get(i);
int len1 = day_string.size();
if (len1 > pos) {
String category = cal_event.getEventCateId();
HashSet<String> eventListSet = new HashSet<String>();
StringTokenizer stR = new StringTokenizer(category, ",");
while (stR.hasMoreTokens()) {
eventListSet.add(stR.nextToken());
}
ArrayList<String> sortedList = new ArrayList(eventListSet1);
Collections.sort(sortedList);
//System.out.println("sortedList:" + sortedList);
StringBuffer CategoryNew = new StringBuffer();
Iterator<String> itr = sortedList.iterator();
while (itr.hasNext()) {
CategoryNew.append(itr.next() + ",");
}
eventList.add(category);
switch (CategoryNew.toString()) {
/* ----------only one category ----------*/
case "1,":
v.setBackgroundResource(R.drawable.gs);
break;
case "2,":
v.setBackgroundResource(R.drawable.se);
break;
case "3,":
v.setBackgroundResource(R.drawable.ge);
break;
case "4,":
v.setBackgroundResource(R.drawable.tuce);
break;
/*----------two category----------*/
case "1,2,":
v.setBackgroundResource(R.drawable.gsse);
break;
case "1,3,":
v.setBackgroundResource(R.drawable.gsge);
break;
case "1,4,":
v.setBackgroundResource(R.drawable.gstuce);
break;
case "2,3,":
v.setBackgroundResource(R.drawable.sege);
break;
case "2,4,":
v.setBackgroundResource(R.drawable.setuce);
break;
case "3,4,":
v.setBackgroundResource(R.drawable.getuce);
break;
/*----------three category----------*/
case "1,2,3,":
v.setBackgroundResource(R.drawable.gssege);
break;
case "1,3,4,":
v.setBackgroundResource(R.drawable.gsgetuce);
break;
case "1,2,4,":
v.setBackgroundResource(R.drawable.gssetuce);
break;
case "2,3,4,":
v.setBackgroundResource(R.drawable.gesetuce);
break;
/*----------four category----------*/
case "1,2,3,4,":
v.setBackgroundResource(R.drawable.gssegetuce);
default:
System.out.println("Invalid event");
break;
}
/*BadgeView1 badge = new BadgeView1(context, cal_item_ll);
badge.setText("1");
badge.show();*/
txt.setTextColor(Color.BLUE);
}
}
}
public void getPositionList(String date, final Activity act) {
final String TAG_EVENT_NAME = "event_name";
final String TAG_EVENT_DESC = "event_desc";
final String TAG_EVENT_FROM_DATE = "event_from_date";
final String TAG_EVENT_TO_DATE = "event_to_date";
final String TAG_EVENT_TIMING = "event_timing";
final String TAG_EVENT_PLACE = "event_place";
final String TAG_EVENT_CATE_ID = "event_cate_id";
String event_name, event_desc, event_from_date;
String event_to_date, event_timing, event_place, event_cate_id;
ArrayList<HashMap<String, String>> event = new ArrayList<>();
int len = CalendarEvent.event_calendar_arr.size();
for (int i = 0; i < len; i++) {
CalendarEvent cal_event = CalendarEvent.event_calendar_arr.get(i);
event_name = cal_event.getEventName();
event_desc = cal_event.getEventDesc();
event_from_date = cal_event.getEventFromDate();
event_to_date = cal_event.getEventToDate();
event_timing = cal_event.getEventTiming();
event_place = cal_event.getEventPlace();
event_cate_id = cal_event.getEventCateId();
if (date.equals(event_from_date)) {
HashMap<String, String> map = new HashMap<>();
map.put(TAG_EVENT_NAME, event_name);
map.put(TAG_EVENT_DESC, event_desc);
map.put(TAG_EVENT_FROM_DATE, event_from_date);
map.put(TAG_EVENT_TO_DATE, event_to_date);
map.put(TAG_EVENT_TIMING, event_timing);
map.put(TAG_EVENT_PLACE, event_place);
if (event_cate_id.equals(1)) {
map.put(TAG_EVENT_CATE_ID, "General Science");
}
if (event_cate_id.equals(2)) {
map.put(TAG_EVENT_CATE_ID, "Scientific Exhibition");
}
if (event_cate_id.equals(3)) {
map.put(TAG_EVENT_CATE_ID, "General Events");
}
if (event_cate_id.equals(4)) {
map.put(TAG_EVENT_CATE_ID, "Tuce Clubs");
}
if (event_cate_id.equals("")) {
map.put(TAG_EVENT_CATE_ID, "");
}
event.add(map);
}
}
if (event.size() > 0) {
Bundle b = new Bundle();
b.putSerializable("EVENT_LIST", event);
Intent in = new Intent(context, Event1.class);
in.putExtras(b);
context.startActivity(in);
} else {
ToastMessage.toastMessage(context, "On this date there is no event");
}
}
BadgeView.class -
package vyClean.wemecalendar.util;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BadgeView extends TextView {
private Context context;
protected View target;
private ShapeDrawable badgeBg;
public BadgeView(Context context, View target) {
this(context, null, android.R.attr.textViewStyle, target);
}
public BadgeView(Context context, AttributeSet attrs, int defStyle, View target) {
super(context, attrs, defStyle);
init(context, target);
}
private void init(Context context, View target) {
try {
this.context = context;
this.target = target;
//apply defaults
setTypeface(Typeface.DEFAULT_BOLD);
setPadding(7, 0, 7, 0);
setTextColor(Color.WHITE);
if (this.target != null) {
applyTo(this.target);
} else {
show();
}
} catch (StackOverflowError e) {
e.printStackTrace();
}
}
private void applyTo(View target) {
try {
ViewGroup.LayoutParams lp = target.getLayoutParams();
//ViewParent parent = target.getParent();
FrameLayout container = new FrameLayout(context);
// TODO verify that parent is indeed a ViewGroup
ViewGroup group = (ViewGroup) target;
int index = group.indexOfChild(target);
group.removeView(target);
group.addView(container, index,lp);
container.addView(target);
container.addView(this);
group.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
public void show() {
try {
if (getBackground() == null) {
if (badgeBg == null) {
badgeBg = getDefaultBackground();
}
setBackgroundDrawable(badgeBg);
}
applyLayoutParams();
} catch (StackOverflowError e) {
e.printStackTrace();
}
}
private ShapeDrawable getDefaultBackground() {
ShapeDrawable drawable = null;
try {
float[] outerR = new float[]{20, 20, 20, 20, 20, 20, 20, 20};
RoundRectShape rr = new RoundRectShape(outerR, null, null);
drawable = new ShapeDrawable(rr);
drawable.getPaint().setColor(Color.BLACK);
} catch (StackOverflowError e) {
e.printStackTrace();
}
return drawable;
}
private void applyLayoutParams() {
try {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.RIGHT | Gravity.TOP;
setLayoutParams(lp);
} catch (StackOverflowError e) {
e.printStackTrace();
}
}
}