Trying to store data into a string.
This is my utilities.
public class CommonDatePicker {
public static void datePicker(final TextView textView, Context context) {
Calendar dateCalender = Calendar.getInstance();
final int year = dateCalender.get(Calendar.YEAR);
final int month = dateCalender.get(Calendar.MONTH);
final int day = dateCalender.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog;
datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
monthOfYear = monthOfYear + 1;
//2001-05-03
// dayOfMonth monthOfYear
String formattedMonth = "" + monthOfYear;
String formattedDayOfMonth = "" + dayOfMonth;
if(monthOfYear < 10){
formattedMonth = "0" + monthOfYear;
}
if(dayOfMonth < 10){
formattedDayOfMonth = "0" + dayOfMonth ;
}
//editText.setText( formattedDayOfMonth + "-" + formattedMonth + "-" + year);
textView.setText( year + "-" + formattedMonth + "-" + formattedDayOfMonth);
}
}, year, month, day);
datePickerDialog.setTitle("SELECT DATE");
datePickerDialog.show();
}
}
This is where I'm trying to get data. After clicking on textView the datePicker appears as expected but I want to store picked data into a string. I don't know how to do it.
This is my main.java
txt_data_collection_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CommonDatePicker.datePicker(txt_data_collection_date, TofsilForm10Activity.this);
//String date; I want to store data in date.
}
});
Caveat, I am not an Android developer, so this may be a silly Answer.
By the way, the terrible
Calendar
class was supplanted years ago by the modern java.time classes defined in JSR 310. Android 26+ comes with an implementation. For earlier Android, the latest tooling makes most of the java.time functionality available via “API desugaring”.Replace this:
… with the following use of
LocalDate
. And note that unlike the legacy classes, java.time uses sane numbering with the year 2022 being 2022, and January-December being 1-12.The format you want for your text happens to comply with ISO 8601 standard. That standard’s formats are used by default in the java.time classes when generating/parsing text. So replace all this:
… with this:
So that first piece of code could look like this. Notice how better indenting makes the code more obvious.
Having revamped that code, let's address your question, which apparently is that you want to save the user's chosen date into another place. I am just guessing at that given (a) your Comments, and (b) I cannot determine the purpose of your second code snippet.
Your
onDateChanged
method reacts to the user choosing a date in the date picker. There you generate your desired text. You store that text intextView
. You could store it somewhere else too.Better yet, store the
LocalDate
on that other place, rather than mere text.Of course, global variables like
someOtherObject.someField
should generally be avoided. So pass it in just like you passed inTextView textView
.Perhaps a better approach is to make use of the
CommonDatePicker
class you have created. Change thatdatePicker
method to be an instance method, not astatic
method. Add a member field to that class to carry the result of a user’s choice. And I would change the name of that method to be more descriptive such asmakeDatePicker
.Kill the
show
call. Interrogate for the result afterwards.Usage:
If the situation is such that the user may cancel the date picker without making a choice, then our
LocalDate
reference variable will be null. You could check for null. But better to return anOptional
to indicate explicitly that null is an acceptable value value to be expected.Usage:
Now we no longer need to pass in
final TextView textView
. Change your class to this.Usage:
After all these changes, notice how we have pulled outside of
CommonDatePicker
all the parts that are extraneous. Now that class can “mind its own business”, without knowing or caring about other pieces of your app. Its job is simply to gather input from the user, not update various parts of your app.We could further simplify the code, so the calling app does not need to know about the use of
DatePickerDialog
. We could drop themakeDatePicker()
, and add ashow
method instead. With ashow
method in place, we can skip the interrogation step, and just haveshow
return theOptional < LocalDate >
object.And rather than assume the dialog should initialize to today's date, accept a date in its constructor.
Usage:
Now the calling programmer need know nothing about the
DatePickerDialog
we happen to be using internally in ourCommonDatePicker
class. The calling programmer needs to learn only our simple API. That API consists of two parts: A constructor taking aContext
and aLocalDate
, and ashow
method that interacts with user to return an optionalLocalDate
object.