Note that there are some explanatory texts on larger screens.

plurals
  1. POSharedPreferences and TimePickerDialog
    primarykey
    data
    text
    <p>Hurro!<br> I'm trying my hand at writing my first Android app, and I've hit a brick wall.</p> <p>I want to allow the user to set a 'start' and a 'stop' time, for a daily schedule. I have sucessfully implemented the TimePickerDialog, and the TextView next to it, but I can't for the LIFE of me figure out how to save the user selected time to a SharedPreferences string.</p> <p>Code is as follows (stripped down for clarity's sake):</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.setup); // Retrieve the shared preferences mUserSettings = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE); // capture our View elements mTimeDisplayStart = (TextView) findViewById(R.id.TextView_ScheduleStart_Info); mPickTimeStart = (Button) findViewById(R.id.Button_ScheduleStart); // add a click listener to the button mPickTimeStart.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_START); } }); // get the current time final Calendar c = Calendar.getInstance(); mHourStart = c.get(Calendar.HOUR_OF_DAY); mMinuteStart = c.get(Calendar.MINUTE); // display the current date updateDisplayStart(); } // updates the time we display in the TextView private void updateDisplayStart() { mTimeDisplayStart.setText( new StringBuilder() .append(padStart(mHourStart)).append(":") .append(padStart(mMinuteStart))); } private static String padStart(int c) { if (c &gt;= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } // the callback received when the user "sets" the time in the dialog private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHourStart = hourOfDay; mMinuteStart = minute; updateDisplayStart(); } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_START: return new TimePickerDialog(this, mTimeSetListenerStart, mHourStart, mMinuteStart, false); } return null; } </code></pre> <p>I assume that the time should be saved at the same time that the TextView is updated... but I have no idea how to do this -.-</p> <p>I've now spent a good 5 or 6 hours looking for an answer, but I literally can't find one. I've experimented with the SharedPreferences.Editor, but that won't work because I need to store the time as a long, not a String.<br> I'm at the end of my rope, and I'd really appreciate some help!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload