forked from upstream-mirrors/OpenTracks
Allows SharedPreferences to contain int as well as String.
This commit is contained in:
@@ -21,7 +21,6 @@ import android.content.SharedPreferences;
|
|||||||
import android.content.SharedPreferences.Editor;
|
import android.content.SharedPreferences.Editor;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.Constants;
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,16 +142,21 @@ public class PreferencesUtils {
|
|||||||
* @param defaultValue the default value
|
* @param defaultValue the default value
|
||||||
*/
|
*/
|
||||||
public static int getInt(Context context, int keyId, int defaultValue) {
|
public static int getInt(Context context, int keyId, int defaultValue) {
|
||||||
//NOTE: We assume that the data was stored as String due to use of ListPreference.
|
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(context);
|
SharedPreferences sharedPreferences = getSharedPreferences(context);
|
||||||
String stringValue = sharedPreferences.getString(getKey(context, keyId), null);
|
|
||||||
|
|
||||||
int intValue = defaultValue;
|
|
||||||
try {
|
try {
|
||||||
intValue = Integer.parseInt(stringValue);
|
return sharedPreferences.getInt(getKey(context, keyId), defaultValue);
|
||||||
} catch (NumberFormatException e) {
|
} catch (ClassCastException e) {
|
||||||
|
//Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
//NOTE: We assume that the data was stored as String due to use of ListPreference.
|
||||||
|
try {
|
||||||
|
String stringValue = sharedPreferences.getString(getKey(context, keyId), null);
|
||||||
|
return Integer.parseInt(stringValue);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return defaultValue;
|
||||||
}
|
}
|
||||||
return intValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user