简体   繁体   English

日期选择器Android显示随机日期

[英]Date Picker Android Show Random Date

Setting up Date picker using Datapicker.update(year,month,day); 使用Datapicker.update设置日期选择器(年,月,日); gives me random date even if the date is correct so any help please I use the same Activity for Add or Edit a Task 给我随机日期,即使日期是正确的所以任何帮助请我使用相同的活动添加或编辑任务

here is my Code 这是我的代码

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    databaseConnector = new DatabaseConnector(this);
    title = (EditText) findViewById(R.id.TitleET);
    subject = (EditText) findViewById(R.id.SubjectET);
    category = (Spinner) findViewById(R.id.CategorySpinner);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Category, android.R.layout.simple_spinner_item);
    category.setAdapter(adapter);
     extra = getIntent().getExtras();
    if(extra !=null)
    {
        String [] spli = extra.getString("Date").split("-");
        row_id = extra.getLong("row_id");
        title.setText(extra.getString("Title"));
        subject.setText(extra.getString("Subject"));
        if(extra.getString("Category").equals("Exam"))
        {
            category.setSelection(0);
        }
        else
        {
            category.setSelection(1);
        }
        datePicker.updateDate(Integer.parseInt(spli[2]),Integer.parseInt(spli[1]),Integer.parseInt(spli[0]));
    }
}

I am not sure if this is the correct approach but i edited this line of code and it works fine 我不确定这是否是正确的方法,但我编辑了这行代码,它工作正常

datePicker.updateDate(Integer.parseInt(spli[2]),Integer.parseInt(spli[1]),Integer.parseInt(spli[0]));
    }

changed to 变成

 datePicker.updateDate(Integer.parseInt(spli[2]),Integer.parseInt(spli[1])-1,Integer.parseInt(spli[0]));

and i dont know the logic behind it but it works 我不知道它背后的逻辑,但它的工作原理

DatePicker.updateDate() 's month argument starts from zero. DatePicker.updateDate()的月份参数从零开始。 From the documentation : 文档

month The month which is starting from zero . 从零开始的月份。

So, as the second argument, use Integer.parseInt(spli[1]) - 1 因此,作为第二个参数,使用Integer.parseInt(spli[1]) - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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