0

Day,year and month using these given values after being updated. I want them to use updated values please help.

var day = 1  
var month = 1  
var year = 1999  
val dpd = DatePickerDialog(this,android.R.style.Theme_Holo_Dialog,
    DatePickerDialog.OnDateSetListener { datePicker, selyear,
   monthOfYear, dayOfMonth ->

    day = dayOfMonth
    month = monthOfYear
    year = selyear
    tv.text = "$day - $month - $year"  /*printing day month year*/

   }, year, month, day
 )

  dpd.show()

   val pday = 30 - day   /* day = 1(Not the chosen value)*/
   val pmonth = 12 - month  
   val pyear = 2019 - year 

1 Answers1

1

Change the variable declarations from val to var and update them inside the listener:

        val c = Calendar.getInstance()
        var day = c.get(Calendar.DAY_OF_MONTH)
        var month = c.get(Calendar.MONTH)
        var year = c.get(Calendar.YEAR)

        val dpd = DatePickerDialog(
            this,
            android.R.style.Theme_Holo_Dialog,
            DatePickerDialog.OnDateSetListener { datePicker, selyear, monthOfYear, dayOfMonth ->
                day = dayOfMonth
                month = monthOfYear + 1
                year = selyear
                tv.text = "$day - $month - $year"

            }, year, month, day
        )

        dpd.show()
forpas
  • 160,666
  • 10
  • 38
  • 76
  • I just wanted to update date month and year according to the selected date in datepicker – Pranjal Rawat Dec 06 '18 at 15:37
  • @PranjalRawat and what is the problem? – forpas Dec 06 '18 at 15:38
  • i wanted to change the value of date month and year according to the select date on date picker but that's not working – Pranjal Rawat Dec 16 '18 at 16:36
  • You still don't say **what** is not working. Is the app crashing? Is `birthDate` not updated? – forpas Dec 16 '18 at 17:54
  • the app is working fine but in the date picker dialog box i am not able to get access to the new selected dates – Pranjal Rawat Dec 18 '18 at 15:02
  • Post the layout of your activity. – forpas Dec 18 '18 at 15:15
  • the date month and year are showing current dates and i wanted the dates selected in datepicker – Pranjal Rawat Dec 18 '18 at 15:27
  • I tested the code: when the datepicker opens the current date is selected. If I choose a date from the datepicker and click ok then the date I selected updates the textview. I can't reproduce your problem. – forpas Dec 18 '18 at 15:31
  • you mean if you print date month and year the selected dates are printed – Pranjal Rawat Dec 18 '18 at 15:39
  • The textview shows the date that I selected from the datepicker. – forpas Dec 18 '18 at 15:42
  • Do you also want the variables day, month and year to change to the selected date? – forpas Dec 18 '18 at 15:44
  • Hey forbas can we talk in gmail as i have some problem in my project – Pranjal Rawat Dec 24 '18 at 13:20
  • @PranjalRawat I don't do any communicating outside of SO. If you have any problem/question about programming post it, with all the relevant info and code and if I can, I will provide an answer. – forpas Dec 24 '18 at 13:27
  • How can i provide you the project files – Pranjal Rawat Dec 26 '18 at 03:10
  • when i am subtracting day, month or year from any number and putting them in new var in this case day, month and year are using there previous values – Pranjal Rawat Jan 03 '19 at 08:15
  • var pday = 30 - day – Pranjal Rawat Jan 03 '19 at 08:16
  • Put: `var pday = 0 var pmonth = 0 var pyear = 0` before the listener and `pday = 30 - day pmonth = 12 - month pyear = 2019 - year` inside the listener, after `tv.text = "$day - $month - $year"` – forpas Jan 03 '19 at 10:06
  • The updated values can be used outside the listener after the dialog is closed, because before they have their original values. If you want to use these values anywhere in the class, put their declarations at the class level. – forpas Jan 03 '19 at 10:41
  • The dialog closes when `OnDateSetListener()` is executed and there you set the values of these variables. Just because you put the lines after the listener this does not mean that the listener has been executed. – forpas Jan 03 '19 at 10:57
  • Help me to create a class to use them outside – Pranjal Rawat Jan 03 '19 at 11:02
  • The comments section is not the place to get answers, just for clarifications. You must post a new question with all the relevant code, describe what you have tried and explain what you exactly need. – forpas Jan 03 '19 at 11:05
  • I just update my question so you can now add a answer to it – Pranjal Rawat Jan 03 '19 at 11:07
  • For the code you updated I gave you the answer: the variables must be inside the listener so they get their values when the user clicks the ok button of the dialog. – forpas Jan 03 '19 at 11:17
  • This is not how it works here in SO. You can't post questions one after another in the same post. You must post a new question where you explain in details what you need and what you have tried so far. – forpas Jan 03 '19 at 11:53