How to update text-view with multiple datepickers

六月ゝ 毕业季﹏ 提交于 2019-12-23 05:17:29

问题


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


回答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;
}



回答2:


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);
        }
    });



回答3:


change END_DATE_DIALOG_ID = 1;

try it should work




回答4:


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



来源:https://stackoverflow.com/questions/20738960/how-to-update-text-view-with-multiple-datepickers

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