I am able to Display the Events Within the List below the Calendar but i want to Display the Events Within the Calendar through the JSON Data.I have used Ca
I am assuming that you want to display the list of events within a month-type calendar. ZCustomCalendar allows you to make custom views for different types of dates. Within that custom view you can define a ListView
and then maybe display the list of events in that.
You will need an EventDecorator class like
public class EventDecorator implements DayViewDecorator {
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));
}
}
Then on the main acitvity/fragment u need to make use of an sync tasks that get json data and set the events in calendar like this sample.
/**
* Shows off the most basic usage
*/
public class BasicActivityDecorated extends AppCompatActivity implements OnDateSelectedListener {
private final OneDayDecorator oneDayDecorator = new OneDayDecorator();
@BindView(R.id.calendarView)
MaterialCalendarView widget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic);
ButterKnife.bind(this);
widget.setOnDateChangedListener(this);
widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
Calendar instance = Calendar.getInstance();
widget.setSelectedDate(instance.getTime());
Calendar instance1 = Calendar.getInstance();
instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
Calendar instance2 = Calendar.getInstance();
instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);
widget.state().edit()
.setMinimumDate(instance1.getTime())
.setMaximumDate(instance2.getTime())
.commit();
widget.addDecorators(
new MySelectorDecorator(this),
new HighlightWeekendsDecorator(),
oneDayDecorator
);
new ApiSimulator().executeOnExecutor(Executors.newSingleThreadExecutor());
}
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
//If you change a decorate, you need to invalidate decorators
oneDayDecorator.setDate(date.getDate());
widget.invalidateDecorators();
}
/**
* Simulate an API call to show how to add decorators
*/
private class ApiSimulator extends AsyncTask<Void, Void, List<CalendarDay>> {
@Override
protected List<CalendarDay> doInBackground(@NonNull Void... voids) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -2);
ArrayList<CalendarDay> dates = new ArrayList<>();
for (int i = 0; i < 30; i++) {
CalendarDay day = CalendarDay.from(calendar);
dates.add(day);
calendar.add(Calendar.DATE, 5);
}
return dates;
}
@Override
protected void onPostExecute(@NonNull List<CalendarDay> calendarDays) {
super.onPostExecute(calendarDays);
if (isFinishing()) {
return;
}
widget.addDecorator(new EventDecorator(Color.RED, calendarDays));
}
}
}
U need to mode this class to include an api call and use that response to bind the dates to calendar. Hope this will help!
You need to add a DotSpan. Also i had to read the library docs cause i haven't used this myself.
Step 1: In your Fragment class
private List<CalendarDay> events = new ArrayList<>();
Step 2 :
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
// i copied your json to load form assets folder
// in our case you can get the json from the server
// or any other location of your choice
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");
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
events.add(day);
}
} catch (Exception e) {
e.printStackTrace();
}
EventDecorator eventDecorator = new EventDecorator(Color.RED, events);
calendarView.addDecorator(eventDecorator);
}
Step 3: Change your EventDecorator class
public class EventDecorator implements DayViewDecorator {
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));
}
}
You can look at the DotSpan and customize it your self. Its a LinearBackgroundSpan
Screen shot on my emulator
Edit:
And specific month should list specific month event rather than all the event ,which is the one i am getting on Currently.
You need to check the month that is currently displayed and make sure you sync your listview to display events accordingly.Clear the list data that is used to display events in list based on the month the calendar displays and refresh your list.
Or you could just display the events for the day on click of date.
How you do it is left to you. You can have a look at this https://github.com/memfis19/Cadar library which should give an insight or help you modify your code accordingly. Based on the month displayed the list displays events.
Edit 2:
My Adapter class
public class MyAdapter extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<Event> list) {
super(context, R.layout.row, 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.row, 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;
}
}
My CalendarFragment
public class CalendarFragment extends Fragment implements OnMonthChangedListener {
private MaterialCalendarView calendarView;
private List<CalendarDay> calevents = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private HashMap<Integer,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
private Calendar cal;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = (ListView)view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),eventList);
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
final Calendar calendar = Calendar.getInstance();
calendarView.setSelectedDate(calendar.getTime());
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
}
});
calendarView.setOnMonthChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
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");
Date date = simpleDateFormat.parse(StartDate);
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = 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(day);
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
EventDecorator eventDecorator = new EventDecorator(Color.RED, calevents);
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 onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
}
}