Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Week View (alamkanak - library) triplicates event names

Good afternoon guys.

I have this problem, when i add some events to my calendar it triplicates the names of then.

i tried changing the name, the time of the events, nothing worked.

please guys help me.

Look at App img

here its my code

package com.example.admin.myapplication;

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.alamkanak.weekview.WeekView;
import com.alamkanak.weekview.WeekViewEvent;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class WeekView_class extends ActionBarActivity implements          WeekView.MonthChangeListener, WeekView.EventClickListener, WeekView.EventLongPressListener{
WeekView mWeekView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_week_view);

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

    // Set an action when any event is clicked.
    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(new WeekView.EventLongPressListener() {
        @Override
        public void onEventLongPress(WeekViewEvent event, RectF eventRect) {

        }
    });
    /*Calendar cal = Calendar.getInstance();
    cal.set(2015, 5 - 1, 30, 12, 00);
    mWeekView.goToDate(cal);*/
    mWeekView.setNumberOfVisibleDays(5);
    mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));//espacio entre columnas
    mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));//tamanho de letra de la api
    mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics()));//tamanho de lettra del evento
    mWeekView.setEventPadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics()));



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_week_view, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.DAY_OF_MONTH, 19);
    startTime.set(Calendar.HOUR_OF_DAY, 1);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.MONTH, 10);
    startTime.set(Calendar.YEAR, 2015);
    Calendar endTime = (Calendar) startTime.clone();
    endTime.set(Calendar.DAY_OF_MONTH, 19);
    endTime.set(Calendar.HOUR_OF_DAY, 5);
    endTime.set(Calendar.MINUTE, 0);
    endTime.set(Calendar.MONTH, 10);
    endTime.set(Calendar.YEAR, 2015);
    WeekViewEvent event = new WeekViewEvent(0, "Epoca", startTime, endTime);
    event.setColor(getResources().getColor(R.color.orange));
    events.add(event);


    startTime = Calendar.getInstance();
    startTime.set(2015, 10, 19, 6, 00);
    endTime = Calendar.getInstance();
    endTime.set(2015, 10, 19, 9, 00);
    WeekViewEvent event2 = new WeekViewEvent(0,"00kjbhjbhjbjbhj",startTime, endTime);
    event2.setColor(getResources().getColor(R.color.orange));
    events.add(event2);


    return events;
}

@Override
public void onEventClick(WeekViewEvent weekViewEvent, RectF rectF) {
    Toast.makeText(this, weekViewEvent.getName().toString()+"\ncon actuacion especial de\n bagulho \nde sao paulo",Toast.LENGTH_SHORT).show();
}

@Override
public void onEventLongPress(WeekViewEvent weekViewEvent, RectF rectF) {

}
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));
}
}

thank you guys!

like image 807
Carlos Montiel Avatar asked Dec 01 '25 03:12

Carlos Montiel


1 Answers

Please add below snippet in your code for display single event at a time

weekView.setMonthChangeListener(new MonthLoader.MonthChangeListener() {
        @Override
        public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
            if (newMonth == mainCalendar.get(Calendar.MONTH))
                return events;
            else {
                return new ArrayList<WeekViewEvent>();
            }
        }
    });

Here I am checking by month . If particular event belong to that month , than only return event array otherwise return empty array :)

Happy coding.

like image 113
Anand Savjani Avatar answered Dec 02 '25 16:12

Anand Savjani