Unable to Current Data on Data picker

微笑、不失礼 提交于 2019-12-20 06:27:59

问题


I Created a Custom Data Picker in which i am showing years from 1950- 2016. now when i first open the picker i want to set current data of the day. but i am not able to set it the current data. after first launch it works exactly as i want it to work.

Here is the Code that I have tried so far

private void openDatePicker(int month, int day, int year) {
        try {

            Log.e("calender", "MONTH = " + month);
            Log.e("calender", "DAY_OF_WEEK = " + day);
            Log.e("calender", "YEAR = " + year);

            m_calendar.set(Calendar.DAY_OF_MONTH, month);
            m_calendar.set(Calendar.MONTH, day);
            m_calendar.set(Calendar.YEAR, year);

            int p_minYear, p_minMonth, p_minDay;
            int p_maxYear, p_maxMonth, p_maxDay;

            p_minDay = 1;
            p_minMonth = 0;
            p_minYear = 1950;

            p_maxDay = 31;
            p_maxMonth = 11;
            p_maxYear = 2016;

            Calendar m_maxDateCalendar = Calendar.getInstance();
            m_maxDateCalendar.set(Calendar.YEAR, p_maxYear);
            m_maxDateCalendar.set(Calendar.MONTH, p_maxMonth);
            m_maxDateCalendar.set(Calendar.DAY_OF_MONTH, p_maxDay);

            Calendar m_minDateCalendar = Calendar.getInstance();
            m_minDateCalendar.set(Calendar.YEAR, p_minYear);
            m_minDateCalendar.set(Calendar.MONTH, p_minMonth);
            m_minDateCalendar.set(Calendar.DAY_OF_MONTH, p_minDay);

            new YearPicker(m_context, m_datePickerListener, m_minDateCalendar,
                    m_maxDateCalendar, m_calendar).show();
        } catch (Throwable p_e) {
            // Have suppressed the exception
            p_e.printStackTrace();
        }
    }

here is the calss that i am using for year Picker YearPicker.Java

public class YearPicker extends DatePickerDialog {
    private int maxYear = 0;
    private int maxMonth = 0;
    private int maxDay = 0;

    private int minYear = 0;
    private int minMonth = 0;
    private int minDay = 0;
    private Calendar m_minDateCalendar, m_maxDateCalendar, m_calendar;

    private boolean m_fired = false;

    public YearPicker(Context context, OnDateSetListener callBack,
            Calendar p_minCalendar, Calendar p_maxCalendar,
            Calendar p_currentCalendar) {
        super(context, callBack, p_currentCalendar.get(Calendar.YEAR),
                p_currentCalendar.get(Calendar.MONTH), p_currentCalendar
                        .get(Calendar.DAY_OF_MONTH));

        m_minDateCalendar = (Calendar) p_minCalendar.clone();
        m_maxDateCalendar = (Calendar) p_maxCalendar.clone();
        m_calendar = (Calendar) p_currentCalendar.clone();
//      m_calendar = p_currentCalendar;

        Log.e("m_calendar", "=> " + m_calendar.get(Calendar.MONDAY));
        Log.e("m_calendar", "=> " + m_calendar.get(Calendar.DAY_OF_WEEK));
        Log.e("m_calendar", "=> " + m_calendar.get(Calendar.YEAR));

        this.minDay = m_minDateCalendar.get(Calendar.DAY_OF_MONTH);
        this.minMonth = m_minDateCalendar.get(Calendar.MONTH);
        this.minYear = m_minDateCalendar.get(Calendar.YEAR);
        this.maxDay = m_maxDateCalendar.get(Calendar.DAY_OF_MONTH);
        this.maxMonth = m_maxDateCalendar.get(Calendar.MONTH);
        this.maxYear = m_maxDateCalendar.get(Calendar.YEAR);

        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                m_fired = false;
                this.getDatePicker().setMaxDate(
                        m_maxDateCalendar.getTime().getTime());
                this.getDatePicker().setMinDate(
                        m_minDateCalendar.getTime().getTime());
            } else {
                m_fired = true;
            }
        } catch (Throwable p_e) {

            Log.d("m_maxDateCalendar", ""
                    + m_maxDateCalendar.getTime().getTime());
            Log.d("m_minDateCalendar", ""
                    + m_minDateCalendar.getTime().getTime());
            Log.d("m_calendar", "" + m_calendar.getTime().getTime());
            p_e.printStackTrace();
        }
    }

    @Override
    public void onDateChanged(DatePicker p_view, int p_year, int p_monthOfYear,
            int p_dayOfMonth) {
        super.onDateChanged(p_view, p_year, p_monthOfYear, p_dayOfMonth);

        if (m_fired) {
            m_calendar.set(Calendar.YEAR, p_year);
            m_calendar.set(Calendar.MONTH, p_monthOfYear);
            m_calendar.set(Calendar.DAY_OF_MONTH, p_dayOfMonth);

            if (m_calendar.after(m_maxDateCalendar)) {
                m_fired = true;
                if (maxMonth > 11) {
                    maxMonth = 11;
                }
                p_view.updateDate(maxYear, maxMonth, maxDay);
            } else if (m_calendar.before(m_minDateCalendar)) {
                m_fired = true;
                if (minMonth > 11) {
                    minMonth = 11;
                }
                p_view.updateDate(minYear, minMonth, minDay);
            } else {
                m_fired = true;
                p_view.updateDate(p_year, p_monthOfYear, p_dayOfMonth);
            }
        }
    }

}

What i want Now

I want to set the current date of the day as picker open up for the first time..

e.g! if user want to select the date then he initially there will be no date selected. but when he will click on date section then he will be prompt with a Date Picker. on that time i want to set Current date of the day. Thanks and please help.


回答1:


I want to set the current date of the day as picker open up for the first time..

IMO, that's because of the following lines:

m_calendar.set(Calendar.DAY_OF_MONTH, month);
m_calendar.set(Calendar.MONTH, day);
m_calendar.set(Calendar.YEAR, year);

It should be as the following:

m_calendar.set(Calendar.DAY_OF_MONTH, day);
m_calendar.set(Calendar.MONTH, month);
m_calendar.set(Calendar.YEAR, year);

or simply call m_calendar.set(year, month, day);

Please note that in Calendar.java file:

public static final int JANUARY = 0;
public static final int FEBRUARY = 1;
...

So if you want FEBRUARY, you should use m_calendar.set(1, month, day); or m_calendar.set(Calendar.FEBRUARY, month, day);. In other words, if you call openDatePicker(3, 20, 2015); the DatePicker will show the date April 20, 2015.

Hope it helps!



来源:https://stackoverflow.com/questions/34404407/unable-to-current-data-on-data-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!