Android Week View(alamkanak - library)将事件名称重复三次

时间:2015-11-19 16:03:32

标签: android calendar

大家下午好。

我有这个问题,当我向我的日历中添加一些事件时,它会使当时的名称重复三次。

我尝试更改名称,事件的时间,没有任何效果。

请伙计们帮帮我。

Look at App img

这是我的代码

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));
}
}
谢谢你们!

2 个答案:

答案 0 :(得分:3)

可能它已经很晚了,你已经把它弄清楚了,但我把这个放在其他人身上。 方法onMonthChange(int newYear, int newMonth)运行三次。实际月份一次,上个月一次,下个月一次。因此,您必须仅为您提供的年/月返回事件。

答案 1 :(得分:3)

请在您的代码中添加以下代码段,以便一次显示单个事件

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

我在这里按月检查。如果特定事件属于该月,则仅返回事件数组,否则返回空数组:)

快乐的编码。