如何在 MaterialCalendarView 中显示特定日期和时间的事件?
How do I display events to a specific date and time in a MaterialCalendarView?
我正在测试一个示例应用程序,我试图在其中实现显示特定 day.Currently 的时间和日期,即使事件是一天,它也只是绘制到整个月。假设事件如果在 1 月 5 日从 9:30am 到 10:30am,它将显示整个月,而不仅仅是 1 月 5 日。想要达到这样的效果:
目前我的应用程序显示了整个月,即使它是 1 月 1 日或 1 月 8 日的事件(这是一张屏幕截图,例如,它错误地显示在 1 月 15 日那一周,即使这 2 个事件属于 2不同的日期):
我正在测试的代码在这里:https://github.com/raghunandankavi2010/SamplesAndroid/tree/master/CalendarTest
唯一的区别是 calendar.xml class 我在 <com.prolificinteractive.materialcalendarview.MaterialCalendarView
下添加了:app:mcv_showOtherDates="all"
以显示每周视图。
此外,在 calendarFragment.java class 中有一个名为 makeJsonObjectRequest 的函数,其中解析了 StartDate,我尝试解析 startTime StartTime 和 EndTime 以及 EndDate 以将事件映射到相应的日期,但那没有不要做太多。
String EndDate = jsonObject.getString("EndTime");
Date date = simpleDateFormat.parse(EndDate);
String title = jsonObject.getString("Title");
Log.d("EndDate ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.DAY_OF_THE_MONTH);
知道如何解决这个问题并更新代码,这样我就可以在特定时间范围内显示每个月的某一天的事件(如上面的 outlook 屏幕截图(第一个屏幕截图)所示),而不是整个月?
(此外,json class i testjson.json 并包含在项目资产文件夹中)
谢谢!
repo 中的示例使用月份过滤事件。您需要按日历日过滤事件并在 onDateSelected
中更新列表视图,以便在用户选择不同日期时更新列表视图。你应该得到这样的东西:
public class CalendarFragment extends Fragment implements OnDateSelectedListener {
private MaterialCalendarView calendarView;
private HashMap<CalendarDay,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),new ArrayList<Event>());
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
calendarView.setSelectedDate(LocalDate.now());
calendarView.setOnDateChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
LocalDate date = LocalDate.parse(StartDate, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss",Locale.US));
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
if(!map.containsKey(day))
{
List<Event> events = new ArrayList<>();
events.add(event);
map.put(day,events);
}else
{
List<Event> events = map.get(day);
events.add(event);
map.put(day,events);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
List<Event> event = map.get(CalendarDay.from(LocalDate.now()));
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
//add small dots on event days
EventDecorator eventDecorator = new EventDecorator(Color.RED, map.keySet());
calendarView.addDecorator(eventDecorator);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("testjson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
List<Event> event = map.get(date);
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
}
}
我正在测试一个示例应用程序,我试图在其中实现显示特定 day.Currently 的时间和日期,即使事件是一天,它也只是绘制到整个月。假设事件如果在 1 月 5 日从 9:30am 到 10:30am,它将显示整个月,而不仅仅是 1 月 5 日。想要达到这样的效果:
目前我的应用程序显示了整个月,即使它是 1 月 1 日或 1 月 8 日的事件(这是一张屏幕截图,例如,它错误地显示在 1 月 15 日那一周,即使这 2 个事件属于 2不同的日期):
我正在测试的代码在这里:https://github.com/raghunandankavi2010/SamplesAndroid/tree/master/CalendarTest
唯一的区别是 calendar.xml class 我在 <com.prolificinteractive.materialcalendarview.MaterialCalendarView
下添加了:app:mcv_showOtherDates="all"
以显示每周视图。
此外,在 calendarFragment.java class 中有一个名为 makeJsonObjectRequest 的函数,其中解析了 StartDate,我尝试解析 startTime StartTime 和 EndTime 以及 EndDate 以将事件映射到相应的日期,但那没有不要做太多。
String EndDate = jsonObject.getString("EndTime");
Date date = simpleDateFormat.parse(EndDate);
String title = jsonObject.getString("Title");
Log.d("EndDate ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.DAY_OF_THE_MONTH);
知道如何解决这个问题并更新代码,这样我就可以在特定时间范围内显示每个月的某一天的事件(如上面的 outlook 屏幕截图(第一个屏幕截图)所示),而不是整个月? (此外,json class i testjson.json 并包含在项目资产文件夹中)
谢谢!
repo 中的示例使用月份过滤事件。您需要按日历日过滤事件并在 onDateSelected
中更新列表视图,以便在用户选择不同日期时更新列表视图。你应该得到这样的东西:
public class CalendarFragment extends Fragment implements OnDateSelectedListener {
private MaterialCalendarView calendarView;
private HashMap<CalendarDay,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),new ArrayList<Event>());
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
calendarView.setSelectedDate(LocalDate.now());
calendarView.setOnDateChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
LocalDate date = LocalDate.parse(StartDate, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss",Locale.US));
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
if(!map.containsKey(day))
{
List<Event> events = new ArrayList<>();
events.add(event);
map.put(day,events);
}else
{
List<Event> events = map.get(day);
events.add(event);
map.put(day,events);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
List<Event> event = map.get(CalendarDay.from(LocalDate.now()));
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
//add small dots on event days
EventDecorator eventDecorator = new EventDecorator(Color.RED, map.keySet());
calendarView.addDecorator(eventDecorator);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("testjson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
List<Event> event = map.get(date);
if(event!=null && event.size()>0) {
adapter.addItems(event);
}else {
adapter.clear();
}
}
}