0

I am trying to update textview when datepicker is selected with some date. But first textview tahat is startDate is not updating it always update second Text-view. I am taking two Date Picker to update two different textview. Here is my code for updating the TextViews

public class AndroidDatePicker extends Activity {

private TextView mStartDate;
private TextView mEndDate;
private Button mStartBtn;
private Button mEndBtn;
int from_year, from_month, from_day, to_year, to_month, to_day;

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;

static final int DATE_PICKER_TO = 0;
static final int DATE_PICKER_FROM = 1;

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

    mStartDate = (TextView) findViewById(R.id.textView1);
    mStartBtn = (Button) findViewById(R.id.button1);

    mStartBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(START_DATE_DIALOG_ID);
        }
    });



    mEndDate = (TextView) findViewById(R.id.textView2);
    mEndBtn = (Button) findViewById(R.id.button2);

    /* add a click listener to the button */
    mEndBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(END_DATE_DIALOG_ID);
        }
    });

    /* get the current date */
    final Calendar c = Calendar.getInstance();
    from_year = c.get(Calendar.YEAR);
    from_month = c.get(Calendar.MONTH);
    from_day = c.get(Calendar.DAY_OF_MONTH);
    to_year = c.get(Calendar.YEAR);
    to_month = c.get(Calendar.MONTH);
    to_day = c.get(Calendar.DAY_OF_MONTH);

    updateEndDisplay();
    updateStartDisplay();
}

private void updateEndDisplay() {
    mEndDate.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(to_month + 1).append("-").append(to_day).append("-")
            .append(to_year).append(" "));
}

private void updateStartDisplay() {
    mStartDate.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(from_month + 1).append("-").append(from_day)
            .append("-").append(from_year).append(" "));
}

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

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        from_year = year;
        from_month = monthOfYear;
        from_day = dayOfMonth;
        updateStartDisplay();
    }
};
private DatePickerDialog.OnDateSetListener to_dateListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        to_year = year;
        to_month = monthOfYear;
        to_day = dayOfMonth;
        updateEndDisplay();
    }
};

@Override
protected Dialog onCreateDialog(int id) {

    switch (id) {
    case DATE_PICKER_FROM:
        return new DatePickerDialog(this, from_dateListener, from_year,
                from_month, from_day);
    case DATE_PICKER_TO:
        return new DatePickerDialog(this, to_dateListener, to_year,
                to_month, to_day);
    }
    return null;
}
}

It is just updating text-view2 but not text-view1. I don't know why. I have followed below link for solution but it is not working in my case i don't know why, Please help me.

DatePicker not updating Textview in Android

Multiple DatePickers in same activity

Community
  • 1
  • 1
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84

4 Answers4

1

you have

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;

change it to

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 1;

otherwise

showDialog(START_DATE_DIALOG_ID);

or

showDialog(END_DATE_DIALOG_ID);

will show the DATE_PICKER_FROM (i.e index=1) dialog only according to your code

protected Dialog onCreateDialog(int id) {

    switch (id) {
    case DATE_PICKER_FROM:
        return new DatePickerDialog(this, from_dateListener, from_year,
                from_month, from_day);
    case DATE_PICKER_TO:
        return new DatePickerDialog(this, to_dateListener, to_year,
                to_month, to_day);
    }
    return null;
}
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • Hey Gaurav, i know this is not the right place to ask you, but can you tell how can i set my endDate date-picker's date based on my first date-picker. – InnocentKiller Dec 23 '13 at 07:56
  • do you wish to display only dates greater than the start date in your date picker? or do you wish to validate upon selection? – gaurav5430 Dec 23 '13 at 07:59
  • i want to display dates greater than start date, and validation like user should not be able to set date lesser than start date. if you just answer this, then this will be great help from your side. – InnocentKiller Dec 23 '13 at 08:06
  • i dont think you can bound the dates in datepicker, but you can compare dates returned after selection to do validation .see http://stackoverflow.com/questions/16029361/setting-upper-and-lower-date-limits-to-date-picker-dialog and http://stackoverflow.com/questions/9494334/how-to-not-allow-user-select-past-date-in-datepicker – gaurav5430 Dec 23 '13 at 08:11
1

As I see,you only need to change the call dialog id,heres the code:

    mStartBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(DATE_PICKER_FROM);
        }
    });
    mEndBtn.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(DATE_PICKER_TO);
        }
    });
Perfume
  • 11
  • 1
0

change END_DATE_DIALOG_ID = 1;

try it should work

Amit
  • 391
  • 3
  • 15
0

just use one DatePickerDialog. Declare a global variable Boolean isFromDate; set it true or false upon showing dialog. In your onDateSet() method check the isFromDate to decide which label to upadte

ben10
  • 180
  • 4
  • 15