mchd
mchd

Reputation: 3163

How to retrieve the user picked Date from DatePicker?

I'm using the Big Nerd Ranch Guide on Android development (2nd Edition). The book is going well so far, it's just that I've noticed a bug in their code and I don't know how to fix it. The onActivityResult method in my CrimeFragment class retrieves the user-picked date and updates the mCrime with that date and also updates the text on the button to display the most current date. This is only possible to do inside the onActivityResult method but the book expects you to paste the line mDateButton.setText(mCrime.getDate().toString()); in a separate activity to be called elsewhere. Well, all that does is to just get back the original date and not the one picked by the user.

So this is the code where the user picks his/her date from the DatePicker dialog:

return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int year = mDatePicker.getYear();
                        int month = mDatePicker.getMonth();
                        int day = mDatePicker.getDayOfMonth();

                        Date date = new GregorianCalendar(year, month, day).getTime();
                        sendResult(Activity.RESULT_OK, date);
                    }
                })
                .setNegativeButton(R.string.cancel, null)
                .create();

And the code sends the result that is picked up by onActivityResult and here is that code:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != Activity.RESULT_OK){
            return;
        }
        if(requestCode == REQUEST_DATE){
            Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            mCrime.setDate(date);
            updateDate();
            /*
            * The code mDateButton.setText(mCrime.getDate().toString()) only works here!!!
            * */
        }
    }

    private void updateDate() {
        mDateButton.setText(mCrime.getDate().toString());
    }

That line of code only works in that method but I was told to put it into a new method called updateDate() and call it where it is necessary. It doesn't work. Very frustrated with this problem. Seems like a very childish mistake for such a popular book.

Thanks.

Upvotes: 0

Views: 496

Answers (1)

GuruCharan
GuruCharan

Reputation: 139

Try below this user picked Date from DatePicker,I hope this will help you.

public void pickdate(View view){
    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);

    final Calendar c1 = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {

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

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            datebutton.setText(dateFormat.format(c1.getTime())); //to set date on button in format yyyy-MM-dd
            String date = datebtn.getText().toString();//to get date to string from button
            //OR use below to assign directly

            /*
            String date = dateFormat.format(c1.getTime());
            */

            /* DateFormat datefmt= DateFormat.getDateInstance(DateFormat.MEDIUM);  // to get date format in Feb 31,3017
                button.setText(datefmt.format(c1.getTime()));
                string = button.getText().toString();*/

        }
    }, mYear, mMonth, mDay);
    datePickerDialog.show();
    long now = System.currentTimeMillis() - 1000;
    datePickerDialog.getDatePicker().setMinDate(now);// To Disable previous dates and enables from present date in datepicker
    datePickerDialog.getDatePicker().setMaxDate(now+31536000000L);// for 365 days  1000*60*60*24*365  mill_sec*sec*min*hours*days
}

All the best,

Upvotes: 1

Related Questions