Вопрос

I am attempting to implement the date picker from http://developer.android.com/guide/topics/ui/controls/pickers.html in order to create a calendar event at a particular date. I grabbed some code from this stackoverflow thread: How to add calendar events in Android? that passes the chosen date out as an event, which launches the calendar app.

However, each time I do this, it passes out two identical events, and I can't figure out why.

Here is the code for my main class:

package com.example.calendartest;

import java.util.Calendar;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.content.Intent;

public class CalendarTest extends FragmentActivity {

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


public void testCalendar(View v) {
    Calendar cal = Calendar.getInstance();              
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    startActivity(intent);
}


public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}
}

And the class that creates the date picker:

package com.example.calendartest;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;


public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}


public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    Calendar cal = Calendar.getInstance(); 
    cal.set(year, month, day);
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", false);
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    startActivity(intent);

    }
}

You'll notice that I have some identical code, which I've been using to test. There are two methods, testCalendar() and showDatePickerDialog() that both contain the code that creates the event. In testCalendar(), the event is created using the current date and time. In showDatePickerDialog(), the event is created using a date and time decided by the date picker.

I set up a button to call testCalendar() and one to call showDatePickerDialog(). The testCalendar() button correctly creates a single event. The showDatePickerDialog() creates two. Why?

Это было полезно?

Решение

This is a known bug, with no workarounds other than to ignore the extra event or roll your own dialog that has a DatePicker in it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top