I have the following as AppCompatSpinner
's entries:
<string-array name="startTimeList">
<item>Now</item>
<item>Pick a time..</item>
</string-array>
Upon selecting Pick a time..
, a TimePickerDialog
is opened and the user is allowed to choose a time. Here's how:
startTimeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// startTimeString = adapterView.getItemAtPosition(i).toString();
DateFormat currentDateFormat = new SimpleDateFormat("HH:mm:ss");
userAvailableTimeInSF = currentDateFormat.format(new Date());
final TextView startTimeSpinnerTV = (TextView) adapterView.getSelectedView();
startTimeSpinnerTV.setText(userAvailableTimeInSF);
switch (i) {
case 0:
userAvailableTimeInSF = currentDateFormat.format(new Date());
startTimeSpinnerTV.setText("Now");
break;
default:
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(PostSportRequest.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));
showTime(hourOfDay, minute);
userAvailableTimeInSF = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
startTimeSpinnerTV.setText(userAvailableTimeAMPM);
Toast.makeText(getBaseContext(), "userAvailableTimeInSF: " + userAvailableTimeInSF, Toast.LENGTH_SHORT).show();
}
}, mHour, mMinute, false);
timePickerDialog.show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Upon selecting Pick a time..
the first time, TimePickerDialog is successfully opened and the chosen time is shown but when I choose it again or click on it again, nothing happens!
I don't know why!
Please let me know how can I get the TimePickerDialog opened and chose the time no matter how many times I select/click it.
i
? and are you getting Toast message on second selection ? – Choralint i
is a parameter inonItemSelected()
and yes, I get theToast
message first time but then if without selectingcase 0
first I selectcase 1
again, nothing happens! As described in the answer below by @MarcinJedynak His approach didn't worked for me! – Vincase 1:
todefault:
and check getting Dialog every time excluding 0 – Choral