Closed shalinipk closed 9 years ago
Sorry. I should have been more precise about the code. Here, check out the new code.
private List<EventModel> mEventModels = new ArrayList<EventModel>();
@Override
public List onMonthChange(int newYear, int newMonth) {
// Check if you've already fetched the events for this month on the previous call.
if (mEventModels.size() == 0 || !containsEvents(mEventModels, newYear, newMonth)) {
// Fetch events for the date user selected.
fetchEventsInBackground();
return new ArrayList<WeekViewEvent>();
}
// If we've reached up to this point then it means we've already fetched the data in the previous call.
return getWeekViewEventsFromEventModels(mEventModels, year, month);
}
// Callback when fetchEvents finishes.
public void setEventList(Activity activity, final List<EventModel> eventModels) {
// Hide the progress dialog.
if(mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
// Refresh the week view.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Store the returned events in a global variable for later use.
mEventModels.addAll(eventModels);
// This line will trigger the method 'onMonthChange()' again.
mWeekView.notifyDatasetChanged();
}
});
}
private boolean containsEvents(List eventModels, year, month) {
// TODO Do a search in the 'eventModels' list to see if it contains the events of the given 'year' and 'month'. If not then return false.
}
private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) {
// TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory.
}
Thank you so much, I will try it out
Hi
I am not able to get your sample.me not work
@saecmca I'd suggest you to learn importing using gradle projects to android studio first.
I am working Android Eclipse sdk
what is the EventModel in List
WeekViewEvent
private List
here i can use WeekViewEvent.
private List<WeekViewEvent> mEventModels = newArrayList<WeekViewEvent>()
thank you...its work. how to design Event like below image https://mail.google.com/mail/u/0/?ui=2&ik=9857f02e0d&view=fimg&th=14e4a2b90a50266b&attid=0.6&disp=inline&realattid=f_ibkup0op18&safe=1&attbid=ANGjdJ8EKb9wgUOpWxlpwrNivFJuS_WUf-7ok0oPWC1gTIFij1IB_a2K4vDVTJs9FvF2EnGc061hkOwReIlLE3DBScHONdjjdTSmHnB48jUAmjTDU08esK-yN9YIwxA&ats=1436415839773&rm=14e4a2b90a50266b&zw&sz=w1256-h523
no permission to view image. But I suggest you take a look in the sample app: https://github.com/alamkanak/Android-Week-View/tree/master/sample
@alamkanak I am trying to implement this library but still can success getting events with asynctask without infinite looping.
The sample code to prevent infinite loop you provided for on MonthChange event is this: if (events.size() == 0 || !containsEvents(events, newYear, newMonth)) {}
But here is my problem; I have events for december but not for january 2016.Because of I don't have any event for next month infinite loop starts because containsEvent for 2106/01 return false. Calender logic prefetches 1 month before and 1 month after. so onMonthChange fires 3 times. First it fires for 2015/12 and gets events, Than 2015/11 and still get events, Than 2016/01 and no events. Each these 3 calls to onMonthChange tries to get events from cloud with asynctask and as a result it calls setEventList() as you wrote. Each response for 105 and 2015/11 calls mWeekView.notifyDatasetChanged(); after mEventModels.addAll(eventModels); And every notifyDatasetChanged call fires onMonthChanges again for all 3 months.
Has anyone success from getting events with asynctask here and can you please share your way todo this?
@nrazon It's best to keep a boolean for every month/year combo if you already created an asynctask to get these events. If you get another request for the same month, you just return an empty list as long as you don't have any events for that month.
how to stop user from scrolling back (older Day than Today) from today?
May I see the fetchEventsInBackground() Async? I am having trouble adding my beans to mEventsModel. Using the mEventModels.addAll(eventModels), what data is stored in eventModels?
@waynem81 You'll probably have to convert your bean to an instance of WeekViewEvent
Thank you @caske33. I was able to figure it out! I currently don't have memory issues, but I am curious how I would implement this portion. I want to be able to remove the events if they are outside of the range.
private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) { // TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory. }
just loop over the events and add the ones that match to a new list and return that list?
This concept turned out to be a decent workaround for async loading of events from Google calendar using content providers.
Hi guys, you can post a complete example to use a http call; I have a custom web API that returns a list of events, but I would make the http call only when there are of new events during scroll future/past.
Your solution?
THX
Check the async example in the repo @fabioDnaweb
There is an error in your code. The return type mentioned here is boolean
and the onMonthChange
method expects a list.
private boolean getWeekViewEventsFromEventModels(List eventModels, year, month) {
// TODO Filter the events from the eventModels by year and month. It would be good idea if you remove the events that are not in the desired range (i.e. where the month is not month+-1). This will release some memory.
}
Hello,
This is with reference to the issue I opened earlier, with the same title. I cannot reopen the issue, because it was not closed by me. Hence opening a new issue:
Question : Is there a way to set the list of events in WeekView other than returning List of WeekViewEvents in onMonthChange() ?
Your solution:
Currently the week view does not provide a ready-made solution for async event loading. But there is a simple hack. Use the following code to achieve what you want.
@Override public List onMonthChange(int newYear, int newMonth) { // Populate the week view with some events. List events = new ArrayList();
}
// Callback when fetchEvents finishes. public void setEventList(Activity activity, final List eventModels) {
}
@Override public List onMonthChange(int year, int month) {
// TODO Be sure to check if the variables year and month equals the year and month of the events of mEventModels.
return getWeekViewEventsFromEventModels(mEventModels);
}
Thank you for the response, however it did not work for me.
Java doesn't support 2 methods with same signature, so I can't have something like this in the same class:
@Override public List onMonthChange(int year, int month) {
// TODO Be sure to check if the variables year and month equals the year and month of the events of mEventModels.
return getWeekViewEventsFromEventModels(mEventModels);
}
@Override
public List onMonthChange(int newYear, int newMonth) {
// Populate the week view with some events.
List events = new ArrayList();
} If mWeekView.notifyDatasetChanged(); triggers onMonthChange() again, then it results in an infinite loop with fetchEventsinBackground() being called again & again!
Is there a better way out ? Thanks for all the help!