简体   繁体   中英

Date Picker Android Show Random Date

Setting up Date picker using Datapicker.update(year,month,day); 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. From the documentation :

month The month which is starting from zero .

So, as the second argument, use Integer.parseInt(spli[1]) - 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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