Я могу отображать события внутри списка под календарю, но я хочу отобразить События в календаре через данные JSON. Я использовал CalendarView для отображения Calendar.I смотрел этот, но не смог решить, что именно я хочу.
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2E353D"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="3dp"
android:src="@mipmap/calander" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Calendar"
android:textColor="#fff"
android:textSize="17dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- <CalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" /> -->
<ListView
android:id="@+id/calenderlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
CalenderFragment
public class CalenderFragment extends Fragment implements OnMonthChangedListener {
String Navigation_URL = "http://192.168.100.5:84/api/academics/getEvents";
String access_token;
ListView listView;
com.prolificinteractive.materialcalendarview.MaterialCalendarView calendarView;
ArrayList<CalenderPojoStudent> student_list_calender = new ArrayList<>();
private HashMap<Integer, List<Event>> map = new HashMap<>();
private List<CalendarDay> calevents = new ArrayList<>();
private Calendar cal;
private List<Event> eventList = new ArrayList<>();
private MyAdapterCalendar adapter;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calender, container, false);
getActivity().setTitle("");
setHasOptionsMenu(true);
calendarView = (com.prolificinteractive.materialcalendarview.MaterialCalendarView) view.findViewById(R.id.calendar);
final Calendar calendar = Calendar.getInstance();
calendarView.setSelectedDate(calendar.getTime());
calendarView.setSelectedDate(calendar.getTime());
listView = (ListView) view.findViewById(R.id.calenderlist);
adapter = new MyAdapterCalendar(getActivity(), eventList);
// listView.setAdapter(adapter);
// calendarView.addDecorator(new HighlightWeekendsDecorator());
calendarView.addDecorator(new OneDayDecorator());
// calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
// calendarView.addView(onMonthChanged(calendarView,date));
// calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
//calendarView.addDecorator(new MySelectorDecorator(getActivity()));
// calendarView.addDecorator(new EventDecorator("#FF0000",student_list_calender));
// calendarView.addDecorator(new EventDecorator("",));
//calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
SessionManagement session = new SessionManagement(getContext());
session.checkLogin();
access_token = session.getAccesstToken();
makeJsonObjectRequest();
return view;
}
// List<Event> events = new ArrayList<>();
private void makeJsonObjectRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
String URL = Navigation_URL;
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
String Title = jsonObject.getString("Title");
try {
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ", "" + date);
CalendarDay day1 = CalendarDay.from(date);
Event event = new Event(date, Title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
if (!map.containsKey(month)) {
List<Event> events = new ArrayList<>();
events.add(event);
map.put(month, events);
} else {
List<Event> events = map.get(month);
events.add(event);
map.put(month, events);
}
calevents.add(day1);
} catch (ParseException e) {
e.printStackTrace();
}
}
//CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
//listView.setAdapter(calenderAdapter);
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
if (event != null && event.size() > 0)
adapter.addItems(event);
listView.setAdapter(adapter);
EventDecorator eventDecorator = new EventDecorator(Color.RED, calevents);
calendarView.addDecorator(eventDecorator);
} catch (JSONException e) {
makeText(getContext(), "Fetch failed!", LENGTH_SHORT).show();
e.printStackTrace();
}
//
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
makeText(getContext(), error.toString(), LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + access_token);
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
/*
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("id", master_id);
map.put("accessID", accessID);
map.put("currentUser", master_id);
return map;
} */
};
requestQueue.add(stringRequest);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.dashboard, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
// CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
// listView.setAdapter(calenderAdapter);
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
if (event != null && event.size() > 0)
adapter.addItems(event);
widget.invalidateDecorators();
}
}
EventDecorator
public class EventDecorator implements DayViewDecorator {
/*
private final Drawable drawable;
private final CalendarDay day;
private final int color;
public EventDecorator(MaterialCalendarView view, Date date, int colors) {
// super(view);
this.day = CalendarDay.from(date);
this.color = colors;
this.drawable = createTintedDrawable(view.getContext(), color);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
if (this.day.equals(day)) {
return true;
}
return false;
}
@Override
public void decorate(DayViewFacade view) {
view.setSelectionDrawable(drawable);
}
private static Drawable createTintedDrawable(Context context, int color) {
return applyTint(createBaseDrawable(context), color);
}
private static Drawable applyTint(Drawable drawable, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrappedDrawable, color);
return wrappedDrawable;
}
private static Drawable createBaseDrawable(Context context) {
return ContextCompat.getDrawable(context, R.drawable.ic_menu_send);
}
*/
private int color;
private HashSet<CalendarDay> dates;
public EventDecorator(int color, Collection<CalendarDay> dates) {
this.color = color;
this.dates = new HashSet<>(dates);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}
@Override
public void decorate(DayViewFacade view) {
view.addSpan(new DotSpan(5, color));
}
}
Событие
public class Event {
private Date date;
private String events;
public Event(Date date, String events) {
this.date = date;
this.events = events;
}
public Date getDate() {
return date;
}
public String getEvents() {
return events;
}
}
MyAdapterCalendar
public class MyAdapterCalendar extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapterCalendar(Context context, List<Event> list) {
super(context, R.layout.activity_calendar_event, list);
this.mInflater = LayoutInflater.from(context);
this.list = list;
}
static class ViewHolder {
TextView text;
}
public void addItems(List<Event> list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_calendar_event, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getEvents());
return convertView;
}
}
activity_calendar_event
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World"
android:textSize="20sp"></TextView>
</RelativeLayout>
JSON
[
{
"EventID": 1011,
"Title": "May Day",
"Description": "Holiday for the International Labour Day",
"StartDate": "2017-05-01T00:00:00",
"StartTime": "00:00:00",
"EndDate": "2017-05-02T00:00:00",
"EndTime": "00:00:00",
"IsFullDay": true,
"IsHoliday": false,
"DateTime": null
},
{
"EventID": 1020,
"Title": "d",
"Description": "",
"StartDate": "2017-04-27T00:00:00",
"StartTime": "05:45:00",
"EndDate": "2017-04-27T00:00:00",
"EndTime": "05:45:00",
"IsFullDay": false,
"IsHoliday": false,
"DateTime": null
},
{
"EventID": 1019,
"Title": "Mother Day",
"Description": "Mother day",
"StartDate": "2017-04-26T00:00:00",
"StartTime": "10:00:00",
"EndDate": "2017-04-26T00:00:00",
"EndTime": "18:00:00",
"IsFullDay": false,
"IsHoliday": false,
"DateTime": null
}
]
Я хочу показать точку в определенную дату, которая отображается как событие. конкретный месяц должен указывать конкретное событие месяца, а не все событие, которое я получаю сейчас.