如何在alamkanak周视图中添加事件

时间:2015-10-06 13:07:43

标签: java android android-library android-calendar

我在我的应用中使用alam kanak week view。我有一个事件,我知道日期,开始时间和结束时间。现在调用什么函数将此事件添加到周视图中。假设我的日期是20-10-2015,时间是17:30到18:30。 我无法将此事件添加到视图中。请提供是否有任何内容。

这是Alam-Kanak添加活动的方式:

Calendar startTime = Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY, 5);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.MONTH, newMonth-1);
        startTime.set(Calendar.YEAR, newYear);
        startTime.add(Calendar.DATE, 1);
        Calendar endTime = (Calendar) startTime.clone();
        endTime.add(Calendar.HOUR_OF_DAY, 3);
        endTime.set(Calendar.MONTH, newMonth - 1);
        WeekViewEvent event = new WeekViewEvent(3, "Test title", startTime, endTime);
        event.setColor(getResources().getColor(R.color.event_color_03));
        CommonItems.events.add(event);

此事件显示出来。 这是我从服务器获取自定义事件的响应:

  

{"错误":false,"约会":[       {         " id":" 1",         " staff_id":null,         " technician_id":" 1",         " name":" Test",         "地址":" Vijayanagar",         " pincode":" 580040",         " mobile":" 9876543210",         "电子邮件":" test@gmail.com",         " date":" 2015-09-22",         "时间":" 16:30:00",         "纬度":null,         "经度":null,         " status":" open",         " created_at":" 2015-09-06 18:04:47",         " updated_at":null       }]}

这是我添加活动的方式:

try {
            JSONObject jo = new JSONObject(response);
            JSONArray ja = jo.getJSONArray("appointments");
            for (int i = 0; i < ja.length(); i++) {
                JSONObject index = ja.getJSONObject(i);

                String name = index.getString("name"); 

                String date = index.getString("date");
                String[] dateSplit = date.split("-");
                int date_year = Integer.parseInt(dateSplit[0]);
                int date_month = Integer.parseInt(dateSplit[1]);
                int date_day = Integer.parseInt(dateSplit[2]);

                String time = index.getString("time");
                String[] timeSplit = time.split(":");
                int time_hour = Integer.parseInt(timeSplit[0]);
                int time_minute = Integer.parseInt(timeSplit[1]);

                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY, 4);
                startTime.set(Calendar.MINUTE, time_minute);
                startTime.set(Calendar.DATE, date_day);
                startTime.set(Calendar.MONTH, date_month);
                startTime.set(Calendar.YEAR, date_year);
                Calendar endTime = (Calendar) startTime.clone();
                endTime.set(Calendar.HOUR_OF_DAY, 4+1);
                endTime.set(Calendar.MINUTE, time_minute);

                WeekViewEvent event = new WeekViewEvent(1, name, startTime, endTime);
                event.setColor(getResources().getColor(R.color.event_color_01));

                CommonItems.events.add(event);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

这不会出现。!我无法理解什么是错的。请帮忙。

3 个答案:

答案 0 :(得分:2)

我通过库中的一点点改变就这样做了:

更改 onSingleTapConfirmed @Override方法:

 @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {

        if (mEventClickListener != null) {
             boolean didSelect = false;
             if (mEventRects != null) {
             List<EventRect> reversedEventRects = mEventRects;
             Collections.reverse(reversedEventRects);

              for (EventRect event : reversedEventRects) {
                        if (event.rectF != null && e.getX() > event.rectF.left
                                                && e.getX() < event.rectF.right
                                                && e.getY() > event.rectF.top
                                                && e.getY() < event.rectF.bottom) {
                                            mEventClickListener.onEventClick(
                                                    event.originalEvent, event.rectF);
                                            playSoundEffect(SoundEffectConstants.CLICK);
                                            didSelect = true;
                                            break;
                                        }
                                    }
                                }
                                if (!didSelect) {
                                    int day_1 = mFirstVisibleDay.get(Calendar.DAY_OF_YEAR);
                                    int day_2 = mLastVisibleDay.get(Calendar.DAY_OF_YEAR);

                                    int chosen_day;

                                    if (day_1 != day_2) {
                                        chosen_day = (int) (day_1 - 1 + Math.round((e.getX()  - mHeaderColumnPadding - (mNumberOfVisibleDays-1)*mColumnGap - mTextSize)
                                                / mWidthPerDay));
                                    } else {
                                        chosen_day = day_1;
                                    }

                                    Calendar RETCAL = Calendar.getInstance();

                                    RETCAL.setTime(mFirstVisibleDay.getTime());

                                    // int totalY = mScroller

                                    int chosen_hour = Math.max(Math.min((int) Math.round((e.getY() - mHeaderTextHeight - mHeaderRowPadding  - mHeaderMarginBottom - mScroller
                                            .getCurrY()) / mHourHeight),23),0);


                                    RETCAL.set(Calendar.DAY_OF_YEAR, chosen_day);
                                    RETCAL.set(Calendar.HOUR_OF_DAY, chosen_hour-1);
                                    RETCAL.set(Calendar.MINUTE, 0);
                                    mEventClickListener.onNonEventSingleTap(RETCAL);


                                }

                            }
          return super.onSingleTapConfirmed(e);
    }

EventClickListener 界面如下所示:

public interface EventClickListener {
       public void onEventClick(WeekViewEvent event, RectF eventRect);
       public void onNonEventSingleTap(Calendar cal);
  }

现在,您将在活动片段类中获取Calendar对象:

@Override
    public void onNonEventSingleTap(Calendar cal) {
      // User cal object as Calendar
    }

希望这会对你有所帮助。

答案 1 :(得分:1)

你好,我有这个代码,它对我有用。

你有两种方法可以添加事件。

问题是当你添加月份变量时,它需要(月-1) 就像你想在7月份添加一个事件一样,你必须插入7-1 = 6;

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;
}
希望它有所帮助。

答案 2 :(得分:0)

问题可能是您的活动已添加,但您已将活动月份设为 startTime.set(Calendar.MONTH, date_month); ,而不是上个月。所以您的活动会在上个月的同一天添加。检查..如果是这样的情况然后只用

替换该行
startTime.set(Calendar.MONTH, date_month+1);