Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Save string as in shared preferences and retrieve it again anywhere in your app.</p> <pre><code>public class PreferencesData { public static void saveStringAsInt(Context context, String key, String value) { SharedPreferences sharedPrefs = PreferenceManager .getDefaultSharedPreferences(context); try { int intValue = Integer.parseInt(value); sharedPrefs.edit().putInt(key, intValue).commit(); } catch (NumberFormatException e) { Log.e("PreferencesData", "Unable to parse to int: " + value) } } public static int getInt(Context context, String key, int defaultValue) { SharedPreferences sharedPrefs = PreferenceManager .getDefaultSharedPreferences(context); return sharedPrefs.getInt(key, defaultValue); } } </code></pre> <p>Usage:</p> <pre><code>// save // do string manipulation before parsing e.g "weeks" = "168" or "3 hours" = "3" (for example substring away first encounter of whitespace) PreferencesData.saveStringAsInt(context, "weeks", "25"); // retrieve int weeks = PreferencesData.getInt(context, "weeks", -1); </code></pre> <p>As for the translation from weeks or "3 hours" to "3", I cannot give advice as it is very dependant on your needs. This should be a seperate method for each use case. </p> <p>There is no reason why the translation should not convert to Integer directly and pass that to a method like PreferencesData.saveInt(), but your question seemed focused on String values, which is why it most likely would be convenient to convert from String to Integer 'on the fly' so to speak.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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