Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with set event #52

Closed
wojtkuju opened this issue Jan 15, 2015 · 31 comments
Closed

Problem with set event #52

wojtkuju opened this issue Jan 15, 2015 · 31 comments

Comments

@wojtkuju
Copy link

Hello, I have 2 dates:
15-01-2015 14:00, timestamp: 1421330400
15-01-2015 15:00, timestamp: 1421334000
and I want to set Event like this:
@OverRide
public List onMonthChange(int newYear, int newMonth) {

    List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();

    GregorianCalendar startTime = new GregorianCalendar();
    startTime.setTimeInMillis(1421330400*1000);
    GregorianCalendar endTime = new GregorianCalendar();
    endTime.setTimeInMillis(1421334000*1000);

    WeekViewEvent event = new WeekViewEvent(1, getEventTitle(startTime), startTime.get(Calendar.YEAR) , startTime.get(Calendar.MONTH), startTime.get(Calendar.DAY_OF_MONTH), startTime.get(Calendar.HOUR_OF_DAY), startTime.get(Calendar.MINUTE), endTime.get(Calendar.YEAR) , endTime.get(Calendar.MONTH), endTime.get(Calendar.DAY_OF_MONTH), endTime.get(Calendar.HOUR_OF_DAY), endTime.get(Calendar.MINUTE));
    event.setColor(getResources().getColor(R.color.event_color_01));
    events.add(event);

    return events;
}

but event don't show on calendar, how to do this?

@entropitor
Copy link
Contributor

Because that WeekViewEvent constructor uses a more logical month index: januari = 1.

But you can construct it using
new WeekViewEvent(1, getEventTitle(startTime), startTime, endTime)

@alamkanak this issue can be closed.

@wojtkuju
Copy link
Author

It's ok?
WeekViewEvent event = new WeekViewEvent(1, getEventTitle(startTime), startTime.get(Calendar.YEAR) , startTime.get(Calendar.MONTH) + 1, startTime.get(Calendar.DAY_OF_MONTH), startTime.get(Calendar.HOUR_OF_DAY), startTime.get(Calendar.MINUTE), endTime.get(Calendar.YEAR) , endTime.get(Calendar.MONTH) +1, endTime.get(Calendar.DAY_OF_MONTH), endTime.get(Calendar.HOUR_OF_DAY), endTime.get(Calendar.MINUTE));

@entropitor
Copy link
Contributor

yes, but I don't see why you don't just pass in the calendar objects. Now the calendar objects are "destructed" by you to be constructed again in the library...

@wojtkuju
Copy link
Author

new WeekViewEvent(1, getEventTitle(startTime), startTime, endTime)
that don't work, event don't show, as it should be properly?

@entropitor
Copy link
Contributor

@wojtkuju
Copy link
Author

Yes it works, but i have timestamp and I don't know how use timestamp.

@entropitor
Copy link
Contributor

Calender start = Calender.getInstance();
start.setTimeInMillis(1421330400000)
Calender end = Calender.getInstance();
end.setTimeInMillis(1421330400000)

events.add(new WeekViewEvent(1, getEventTitle(start), startTime, endTime))

PS remember that the newMonth from the onMonthChangeListener is also 1-based instead of 0-based.

@wojtkuju
Copy link
Author

Still don't work:
Calendar start = Calendar.getInstance();
start.setTimeInMillis(1421330400000L);
Calendar end = Calendar.getInstance();
end.setTimeInMillis(1421330400000L);

    events.add(new WeekViewEvent(1, getEventTitle(start), start, end));

@entropitor
Copy link
Contributor

end.setTimeInMillis(1421334000L);

You were passing in an event that last for 0 milliseconds.

@wojtkuju
Copy link
Author

14213304000 = Tue, 26 May 2420 20:00:00 GMT

@entropitor
Copy link
Contributor

I meant end.setTimeInMillis(1421334000000L);

=>
start.setTimeInMillis(1421330400000L);
end.setTimeInMillis(1421334000000L);

@wojtkuju
Copy link
Author

When i set like this:
start.setTimeInMillis(1421330400000L);
end.setTimeInMillis(1421334000000L);
my calendar view it's like this:
http://oi59.tinypic.com/4ine5c.jpg

@wojtkuju
Copy link
Author

Why when I set event like this:
@OverRide
public List onMonthChange(int newYear, int newMonth) {

    List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();

    Calendar  start = Calendar.getInstance();
    start.set(Calendar.HOUR_OF_DAY, 16);
    start.set(Calendar.MINUTE, 0);
    start.set(Calendar.DAY_OF_MONTH, 15);
    start.set(Calendar.MONTH, 2);
    start.set(Calendar.YEAR, 2015);
    Calendar end = (Calendar) start.clone();
    end.set(Calendar.HOUR_OF_DAY, 17);


    WeekViewEvent event = new WeekViewEvent(10, getEventTitle(start), start, end);
    event.setColor(getResources().getColor(R.color.event_color_03));
    events.add(event);

    return events;
}

calendar views like this:
http://oi59.tinypic.com/10saihc.jpg

@entropitor
Copy link
Contributor

onMonthChange is called three times (when you don't scroll)
This is in the README!

Please remember that the calendar pre-loads events of three consecutive months to enable lag-free scrolling.

That's why it's being shown multiple times.

(You have to take in account the newMonth and newYear variables. They are there for a reason)

@wojtkuju
Copy link
Author

so I do not think it should be?

@wojtkuju
Copy link
Author

I really do not know how I set the event for example 15-01-2015. So that two months after scrolling forward and return to the event, was displayed only once?

@entropitor
Copy link
Contributor

just only add it if newMonth==1 and newYear==2015

@wojtkuju
Copy link
Author

This is a bad idea, because I have list with dates, ie: 15-01-2015, 23-03-2015, 07-06-2015...and I have set all dates.

@entropitor
Copy link
Contributor

This seriously doesn't belong in an issue for this library.

Just only add the events which have a matching month and year (matching = event falls in the month that is requested by newMonth and newYear: event.getMonth() == newMonth && event.getYear() == newYear). That's what I am saying.
When you scroll, eventually the right month will be requested and the event will be loaded.

@wojtkuju
Copy link
Author

Please show me example.

@entropitor
Copy link
Contributor

Remember this is pseudo-code, not real java code

weekviewEvents = new ArrayList()
for(Event event : allEvents){
    if(event.getMonth() == newMonth && event.getYear() == newYear){
         weekviewEvents.add(event)
    }
}
return weekviewEvents

@wojtkuju
Copy link
Author

This is my code, event don't show:
List events = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Get a reference for the week view in the layout.
    mWeekView = (WeekView) findViewById(R.id.weekView);

    DateTimeInterpreter dateTimeInterpreter = new DateTimeInterpreter() {
        @Override
        public String interpretDate(Calendar calendar) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            return simpleDateFormat.format(calendar.getTime());
        }

        @Override
        public String interpretTime(int i) {
            return String.valueOf(i + ":00");
        }
    };
    mWeekView.setDateTimeInterpreter(dateTimeInterpreter);
    mWeekView.setOnEventClickListener(this);
    mWeekView.setMonthChangeListener(this);
    mWeekView.setNumberOfVisibleDays(1);

    // Lets change some dimensions to best fit the view.
    mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()));
    mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
    mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));


    Calendar startTime = Calendar.getInstance();
    Calendar endTime = Calendar.getInstance();

    startTime.setTimeInMillis(1421506800 * 1000L);
    endTime.setTimeInMillis(1421510400 * 1000L);
    WeekViewEvent event = new WeekViewEvent(1, "fddsdsdsd", startTime, endTime);
    event.setColor(getResources().getColor(R.color.event_color_01));
    events.add(event);
}

@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    List<WeekViewEvent> weekviewEvents = new ArrayList<WeekViewEvent>();
    for (WeekViewEvent event : events) {
        if(event.getStartTime().get(Calendar.MONTH) == newMonth && event.getStartTime().get(Calendar.YEAR) == newYear){
            weekviewEvents.add(event);
        }
    }

    return weekviewEvents;
}

@entropitor
Copy link
Contributor

How many times do I need to say that newMonth is 1-based and Calendar is 0-based?

=> event.getStartTime().get(Calendar.MONTH)+1 == newMonth

@wojtkuju
Copy link
Author

Sorry my fault, now i have that code:
Calendar startTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();

    startTime.setTimeInMillis(1421506800 * 1000L);
    endTime.setTimeInMillis(1421510400 * 1000L);
    WeekViewEvent event = new WeekViewEvent(1, "first event", startTime, endTime);
    event.setColor(getResources().getColor(R.color.event_color_01));
    events.add(event);

    startTime.setTimeInMillis(1421596800 * 1000L);
    endTime.setTimeInMillis(1421600400 * 1000L);
    event = new WeekViewEvent(1, "second event", startTime, endTime);
    event.setColor(getResources().getColor(R.color.event_color_01));
    events.add(event);
}

@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    List<WeekViewEvent> weekviewEvents = new ArrayList<WeekViewEvent>();
    for (WeekViewEvent event : events) {
        if(event.getStartTime().get(Calendar.MONTH)+1 == newMonth && event.getStartTime().get(Calendar.YEAR) == newYear){
            weekviewEvents.add(event);
        }
    }

    return weekviewEvents;
}

and my calendar look like this
http://oi57.tinypic.com/14xz790.jpg

@entropitor
Copy link
Contributor

I'm guessing you should pass in a different id for the second event...

@wojtkuju
Copy link
Author

I change id and doesn't work.

@entropitor
Copy link
Contributor

you have to create new instances of the Calendar object (startTime, endTime) for your second event, otherwise you change the time of the first event too.

@KimPark89
Copy link
Contributor

Yes, caske33's answer is right. I had a similar problem and I lost about half day of work to resolve it.

Btw i don't understand why this API preloads 3 months events.
You must add your events by matching your actual month and year with nextMonth and nextYear variables of onMonthChange for avoiding the creations of duplicate events, but in this way you lose the benefit of the 3 months preload.
So probably it's better adding the matching also for your previous and following month events, but in this case you have again the "duplicate events problem".

@entropitor
Copy link
Contributor

The reason why there's a 3 month preload is to smooth the swiping. This way the library won't have to wait for you to provide the events when the user scrolls to the next month, they will already be loaded.

And most people don't match all their events with the nextMonth and nextYear variables but they load their events based on those variables (e.g. they look in the database for the events that fall within the month requested)

@KimPark89
Copy link
Contributor

Thank you, i undestand.

@alamkanak
Copy link
Owner

Answer from @caske33 is correct

udaysrinath pushed a commit to udaysrinath/Android-Week-View that referenced this issue Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants