alamkanak / Android-Week-View

Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.
Apache License 2.0
3.42k stars 1.23k forks source link

Remove events #149

Closed JHNBOS closed 9 years ago

JHNBOS commented 9 years ago

Hello everyone,

If i would like to remove an event, how should i do this?

Thanks

lolobosse commented 9 years ago

Remove it from the list you provided in the onMonthChange and call notifyDataSetChanged

JHNBOS commented 9 years ago

You mean something like this? I put this in the onEventClick method.

events.remove(event); mWeekView.notifyDatasetChanged();

when i click on an event is gives a NullpointerException

lolobosse commented 9 years ago

Is your view already populated? events seems to be null

entropitor commented 9 years ago

Indeed. either events or mWeekView is null.

JHNBOS commented 9 years ago

Well, the date of the events are pulled from a database and are used to create the events. I got an arraylist where i put this data in and then use that data to create an event.

entropitor commented 9 years ago

If you get a NullPointerException, either events or mWeekView is null (or the error is in code that you're not showing)

lolobosse commented 9 years ago

Show the code, it'll much easier :smile:

JHNBOS commented 9 years ago

package nl.mad_world.chilltime;

/**

import android.content.Intent; import android.graphics.RectF; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast;

import com.alamkanak.weekview.DateTimeInterpreter; import com.alamkanak.weekview.WeekView; import com.alamkanak.weekview.WeekViewEvent; import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery;

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.TimeZone;

public class WeekViewer extends ActionBarActivity implements WeekView.MonthChangeListener, WeekView.EventClickListener, WeekView.EventLongPressListener, WeekView.ScrollListener {

private static final int TYPE_DAY_VIEW = 1;
private static final int TYPE_THREE_DAY_VIEW = 2;
private static final int TYPE_WEEK_VIEW = 3;
public ArrayList<ParseObject> activityArray = new ArrayList<>();
private int mWeekViewType = TYPE_THREE_DAY_VIEW;
private WeekView mWeekView;
public List<WeekViewEvent> events;

public void getData() {
    //METHOD OM ALLE EVENTS VAN PARSE.COM OP TE HALEN EN ZET DEZE IN EEN ARRAYLIST.
    Intent getGroup = getIntent();
    String select = getGroup.getExtras().getString("selectedGroup");

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
    query.whereExists("Title");
    query.whereEqualTo("Group", select);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> List, ParseException e) {
            try {
                if (e == null) {

                    Log.d("Afspraak", "Opgehaald " + List.size() + " afspraken");

                    for (int i = 0; i < List.size(); i++) {
                        activityArray.add(List.get(i));
                    }

                } else {
                    Log.d("Afspraken", "Error: " + e.getMessage());
                }
            } catch (Exception t) {
                Toast.makeText(getApplicationContext(),
                        "Kan geen afspraken ophalen!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

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

    // ELEMENT INSTELLEN IN XML
    mWeekView = (WeekView) findViewById(R.id.weekView);

    // ALS JE OP EVENT CLICKT KRIJG JE EEN TOAST.
    mWeekView.setOnEventClickListener(this);

    // The week view has infinite scrolling horizontally. We have to provide the events of a
    // month every time the month changes on the week view.
    mWeekView.setMonthChangeListener(this);

    // Set long press listener for events.
    mWeekView.setEventLongPressListener(this);

    mWeekView.setScrollListener(this);

    // Set up a date time interpreter to interpret how the date and time will be formatted in
    // the week view. This is optional.
    setupDateTimeInterpreter(true);

    getData();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.week, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    setupDateTimeInterpreter(id == R.id.action_week_view);
    switch (id) {
        case R.id.eventcreate:

            Intent getSel = getIntent();
            String selected = getSel.getExtras().getString("selectedGroup");

            Intent createEvent = new Intent(this, Event.class);
            createEvent.putExtra("Groups", selected);
            startActivity(createEvent);
            return true;

        case R.id.action_today:
            mWeekView.goToToday();
            return true;
        case R.id.action_day_view:
            if (mWeekViewType != TYPE_DAY_VIEW) {
                item.setChecked(!item.isChecked());
                mWeekViewType = TYPE_DAY_VIEW;
                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()));
            }
            return true;

        case R.id.action_three_day_view:
            if (mWeekViewType != TYPE_THREE_DAY_VIEW) {
                item.setChecked(!item.isChecked());
                mWeekViewType = TYPE_THREE_DAY_VIEW;
                mWeekView.setNumberOfVisibleDays(3);

                // 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()));
            }
            return true;
        case R.id.action_week_view:
            if (mWeekViewType != TYPE_WEEK_VIEW) {
                item.setChecked(!item.isChecked());
                mWeekViewType = TYPE_WEEK_VIEW;
                mWeekView.setNumberOfVisibleDays(7);

                // Lets change some dimensions to best fit the view.
                mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
                mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));
                mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));
            }
            return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Set up a date time interpreter which will show short date values when in week view and long
 * date values otherwise.
 *
 * @param shortDate True if the date values should be short.
 */
private void setupDateTimeInterpreter(final boolean shortDate) {
    mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() {
        @Override
        public String interpretDate(Calendar date) {
            SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEEE");
            String weekday = weekdayNameFormat.format(date.getTime());
            SimpleDateFormat format = new SimpleDateFormat("dd MMMM");

            // All android api level do not have a standard way of getting the first letter of
            // the week day name. Hence we get the first char programmatically.
            // Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657

            return weekday + " " + format.format(date.getTime());
        }

        @Override
        public String interpretTime(int hour) {
            if (hour == 24) hour = 0;
            if (hour == 0) hour = 0;
            return hour + ":00";
        }
    });
}

@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    // Populate the week view with some events.
    List<WeekViewEvent> events = new ArrayList<>();

    //Create Event
    int num = 0;
    int id = 0;

    for (int i = 0; i < activityArray.size(); i++) {
        String Title = activityArray.get(i).get("Title").toString();
        Date Start = (Date) activityArray.get(i).get("StartDate");
        Date End = (Date) activityArray.get(i).get("EndDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

        Calendar startTime = Calendar.getInstance(TimeZone.getTimeZone("CEST"));
        startTime.setTime(Start);

        Calendar endTime = (Calendar) startTime.clone();
        endTime.setTime(End);

        WeekViewEvent event = new WeekViewEvent(++id, Title, startTime, endTime);
        mWeekView.setDefaultEventColor(getResources().getColor(R.color.event_color_03));
        events.add(num++, event);
        mWeekView.notifyDatasetChanged();
    }

    mWeekView.notifyDatasetChanged();
    return events;

}

private String getEventTitle(Calendar time) {
    return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH));
}

@Override
public void onEventClick(WeekViewEvent event, RectF eventRect) {
        events.remove(event);
        mWeekView.notifyDatasetChanged();
}

@Override
public void onEventLongPress(WeekViewEvent ev, RectF eventRect) {
}

@Override
public void onFirstVisibleDayChanged(Calendar calendar, Calendar calendar1) {
    mWeekView.notifyDatasetChanged();
}

}

lolobosse commented 9 years ago

Try to change List<WeekViewEvent> events = new ArrayList<>(); to events = new ArrayList<>();

entropitor commented 9 years ago

@sachitkarki first of all, open a new issue for a new topic.

Second of all, there is already an issue for a constant number of days. It's currently not possible yet. Maybe in the future, but currently, you're best bet would be to implement it yourself.

anitharaja commented 7 years ago

How to display My list of names in the place of timings ?. You have displayed the Timings(12 AM to 11 PM), in that place I like to pass an array of values and those values to be replaced with the timings. Where and what should I do. Thanks in advance.

JHNBOS commented 7 years ago

Not sure how to do that, but I would have looked at the dateTimeInterpreter methode. Thats the methode where you can setup the output of the hours on the left. Maybe start there

anitharaja commented 7 years ago

Thanks for your time. Well, I have changed the return value as a static value. But the static value is displayed for 23 times (hours). But What I should get is, only the static value which I have passes should be displayed. The remaining 22 times should not be displayed.

I changed the return value in this "String interpretTime() " function. Please suggest me what am I missing. Thanks.