Michael Zeuner
Michael Zeuner

Reputation: 1776

Saving integers using SharedPreferances in Android

I know how to save string from EditTexts on android but my problem is: I need to save the number of hours someone worked... This is a section of the code I have done... There is an error on the toInt method.

try {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("Hours",edit_hours.getText().toInt());
    editor.commit();

    trying_to_save.setText("SAVED!");
}
catch (Exception e) {
    trying_to_save.setText("I crashed");
}

Could you help me with my problem? Thanks Michael!

Upvotes: 0

Views: 246

Answers (2)

Helal Ismail
Helal Ismail

Reputation: 528

you can use this as an example :

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
EditTextPreference XXX = (EditTextPreference)findPreference("Hours");
XXX.setText(Integer.parseInt(hours));

Upvotes: 0

SiN
SiN

Reputation: 3754

Okay I'm not aware there is a toInt() method in Android?

Try this:

editor.putInt("Hours",Integer.parseInt(edit_hours.getText().toString()));

Upvotes: 2

Related Questions