TrippySquidsman
TrippySquidsman

Reputation: 25

SharedPreferences and TimePickerDialog

Hurro!
I'm trying my hand at writing my first Android app, and I've hit a brick wall.

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.

Code is as follows (stripped down for clarity's sake):

@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 >= 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;
    }

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 -.-

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.
I'm at the end of my rope, and I'd really appreciate some help!!

Upvotes: 0

Views: 1682

Answers (4)

Android
Android

Reputation: 1427

this one code for edit to SharedPreference file :-

  1. private SharedPreferences myPrefs; myPrefs = this.getSharedPreferences("filename",MODE_WORLD_WRITEABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim()); prefsEditor.commit();`

this one code for get to values SharedPreference file :-

  1. myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); String X = myPrefs.getString("Mobile_no", "");

X == get information

Upvotes: 0

Chris Fei
Chris Fei

Reputation: 1317

SharedPreferences supports longs. You can do something like

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE); prefs.edit().putLong("longKey", longValue).commit();

to store the value. To retrieve it, you can do

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE); long longValue = prefs.getLong("longKey", 0l);

Upvotes: 0

Dharmendra
Dharmendra

Reputation: 33996

You have to do like this

private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHourStart = hourOfDay;
                mMinuteStart = minute;
                updateDisplayStart();

                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                cal.set(Calendar.MINUTE, minute);
                mUserSettings.edit().putLong("starttime", cal.getTimeInMillis()).commit();

            }
        };

Upvotes: 1

user1203673
user1203673

Reputation: 1015

in the following method

    // updates the time we display in the TextView
         private void updateDisplayStart() {
            mTimeDisplayStart.setText(
                    new StringBuilder()
                    .append(padStart(mHourStart)).append(":")
                    .append(padStart(mMinuteStart)));
        });

     SharedPreferences settingsActivity = getSharedPreferences("todolist1",
                    Activity.MODE_PRIVATE);
            SharedPreferences.Editor prefEditor = settingsActivity.edit();

            prefEditor.putString("DateToDisplay",new StringBuilder()
                    .append(padStart(mHourStart)).append(":")
                    .append(padStart(mMinuteStart));  // updates the time we display in the TextView



        prefEditor.commit();
}

After this where ever u want retrieve the data from the shared preference as follows

SharedPreferences settingsActivity = getSharedPreferences("todolist1",
                Activity.MODE_PRIVATE);

        if (settingsActivity.contains(DateToDisplay)) {
            String saveddate = settingsActivity
                    .getString(DateToDisplay, "");

Upvotes: 2

Related Questions