DatePickerDialog不会持久化String / crashing

时间:2016-03-21 22:23:41

标签: java android datepicker

我修改了一个我在这里找到的TimePicker,而不是使用DatePicker。 TimePicker在指定时间内以长格式存储毫秒时可以完美地工作,但是,更改为SimpleDateFormat并存储字符串变得非常错误。

这是我的代码,

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;

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

public class DateSelector extends DialogPreference
{
    private Calendar calendar;
    private android.widget.DatePicker picker = null;

    public DateSelector(Context ctxt) {
        this(ctxt, null);
    }
    public DateSelector(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, android.R.attr.dialogPreferenceStyle);
    }
    public DateSelector(Context ctxt, AttributeSet attrs, int defStyle) {
        super(ctxt, attrs, defStyle);
        setPositiveButtonText("Save");
        setNegativeButtonText("Cancel");
        calendar = Calendar.getInstance();
    }
    @Override
    protected View onCreateDialogView() {
        picker = new android.widget.DatePicker(getContext());
        return (picker);
    }
    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        picker.updateDate(
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    }
    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult)
        {
            updateCalendar( picker.getDayOfMonth(), picker.getMonth(), picker.getYear() );

            setSummary(getSummary());

            if (callChangeListener(calendar.getTimeInMillis() )) {
                SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
                //persistString(format.format(calendar.getTime() );
                notifyChanged();
            }
        }
    }
    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return (a.getString(index));
    }
    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
    {
        if (restoreValue)
        {
            if (defaultValue == null) {
                updateCalendar(
                        calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
            } else {
                String[] split = defaultValue.toString().split("-");
                updateCalendar(
                        Integer.parseInt(split[1]), Integer.parseInt(split[0]), Integer.parseInt(split[2]));
            }
        } else {
            if (defaultValue == null) {
                updateCalendar(
                        calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
            } else {
                String[] split = defaultValue.toString().split("-");
                updateCalendar(
                        Integer.parseInt(split[1]), Integer.parseInt(split[0]), Integer.parseInt(split[2]));
            }
        }
        setSummary(getSummary());
    }
    public void updateCalendar( int day, int month, int year ) {
        calendar.set(Calendar.DAY_OF_MONTH, day );
        calendar.set(Calendar.MONTH, month );
        calendar.set(Calendar.YEAR, year);
    }
    @Override
    public CharSequence getSummary() {
        if (calendar != null) {
            SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
            return format.format(calendar.getTime());
        }
        return null;
    }
}

对于我的生活,我无法让它工作,因为它崩溃了persistString方法。我的印象是我正确地使用了这个。有没有人对这个问题有一些智慧?

这是我的logcat:

  

java.lang.ClassCastException:java.lang.Long无法强制转换为   java.lang.String中                                                                                     在   android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)                                                                                     在   android.preference.Preference.getPersistedString(Preference.java:1429)                                                                                     在android.preference.Preference.persistString(Preference.java:1398)

我确定为什么它会在我尝试存储字符串时给出它。

这里也是首选项屏幕,

    <Utilities.DateSelector
        android:title="Year / Month / Day "
        android:key="date"
        android:dependency="birthday"/>

感谢。

1 个答案:

答案 0 :(得分:0)

问题是该方法需要一个String,但是你要处理它很长时间。试试这个:

persistString("" + format.format(calendar.getTime() );

该代码行将long值转换为String。