简体   繁体   中英

how to put current date and day of week on DatePickerDialog

I've an app that uses the DatePickerDialog to set a date. If you change the date on the spinner i would like the date and day of week to be shown on the widget. This does happen when run on a samsung mini with 2.3.3. However when compiled on htc one x running 4.0.4 the date and day of week is not there.

How can i ensure that the date and day of week is always present on the widget regardless of which device or android version?

I need to override the default behavior of the widget as it's not consistent depending on which device or OS. thanks

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date

            return new DatePickerDialog(this, datePickerListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;




            Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
            i.putExtra("nextRota", formattedSpinnyTime);
            i.setAction("NEXT_ROTA"); 
            startActivity(i);

        }
    };

.

[edit]

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date

            //return new DatePickerDialog(this, datePickerListener, year, month, day);
            DatePickerDialog dpd = new DatePickerDialog(nfcscannerapplication, datePickerListener, year, month, day);
            dpd.updateDate(year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;





            DateTime spinnyTime = new DateTime(year, month+1, day, 1, 1);
            Log.e(TAG, "spinnyTime = " + spinnyTime);
            DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
            String formattedSpinnyTime = fmt.print(spinnyTime);
            Log.e(TAG, "spinnyTime = " + formattedSpinnyTime);
            Log.e(TAG, "year = " + year);
            Log.e(TAG, "month = " + month);
            Log.e(TAG, "day = " + day);
            nfcscannerapplication.setPreviousGlobalDateTime(nfcscannerapplication.getglobalDateTime());
            nfcscannerapplication.setGobalDateTime(spinnyTime);



            Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
            i.putExtra("nextRota", formattedSpinnyTime);
            i.setAction("NEXT_ROTA"); 
            startActivity(i);

        }
    };

[edit2]

11-06 16:27:33.320: E/AndroidRuntime(24628): FATAL EXCEPTION: main
11-06 16:27:33.320: E/AndroidRuntime(24628): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:595)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:406)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:320)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.app.Dialog.show(Dialog.java:301)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.app.Activity.showDialog(Activity.java:2983)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.app.Activity.showDialog(Activity.java:2933)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.carefreegroup.GetRota.onOptionsItemSelected(GetRota.java:239)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.app.Activity.onMenuItemSelected(Activity.java:2571)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:981)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:495)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:127)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.view.View$PerformClick.run(View.java:14319)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.os.Handler.handleCallback(Handler.java:608)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.os.Looper.loop(Looper.java:156)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at android.app.ActivityThread.main(ActivityThread.java:5045)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-06 16:27:33.320: E/AndroidRuntime(24628):    at dalvik.system.NativeStart.main(Native Method)

You can use joda-time-2.0.jar api. This api has Datetime class. You can get now time from this class.

DateTime datetime = DateTime.now();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM