如何在共享首选项中保存值并在微调器中设置
how to Save value in Shared Preference and set in spinner
我知道这个问题已经被问过很多次了,但我不明白如何设置,因为我是 android 的新手。
我试过这个:
1. saved prefrence
2. also use this
这是我的代码:
SharedPreferences.Editor editor = sharedpreferences.edit();
String gender = Gender.get(i).toString();
editor.putString(Gender1, gender);
editor.commit();
list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// int selectedPosition = gender.getSelectedItemPosition();
gender.setSelection(sharedpreferences.getInt("Gender1", -1));
list.indexOf(sharedpreferences.getString(Gender1, "Gender1"));
gender.setAdapter(dataAdapter);
编辑:-
我想在我已经存储在首选项中的微调器中设置存储值。
请帮助我。
我想在用户保存他的帐户详细信息时存储此值
提前致谢。
自行解决:
fgender= sharedpreferences.getString(Gender1, "Gender1");
if(fgender.equals("Female")){
gender.setSelection(1);
}else
{
gender.setSelection(0);
}
再次感谢大家的回答。
这是我的 EasyPref class
public class EasyPref {
public EasyPref(Context context) {
sp = context.getSharedPreferences("com.example", Context.MODE_PRIVATE);
}
public void write(final String key, final String value) {
sp.edit().putString(key, value).commit();
}
public void write(final String key, final boolean value) {
sp.edit().putBoolean(key, value).commit();
}
public void write(final String key, final int value) {
sp.edit().putInt(key, value).commit();
}
public void write(final String key, final Set<String> value) {
sp.edit().putStringSet(key, value).commit();
}
public String read(final String key, final String alt) {
return sp.getString(key, alt);
}
public boolean read(final String key, final boolean alt) {
return sp.getBoolean(key, alt);
}
public int read(final String key, final int alt) {
return sp.getInt(key, alt);
}
public Set<String> read(final String key, final Set<String> alt) {
return sp.getStringSet(key, alt);
}
private SharedPreferences sp;
}
只需输入您案例的关键和价值。
SharedPreferences.Editor editor = getEditor(context);
editor.putString("KEY", "VALUE");
editor.apply();
你可以这样保存
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Gender1", -1);
editor.apply();
并从首选项中检索保存的值
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getInt("Gender1", 0);
复制并使用此 classs,通过使用此您可以轻松保存和检索共享首选项值..
/**
* This class handles the all the shared preference operation.
* .i.e., creating shared preference and to set and get value.
*
* @author Jeevanandhan
*/
public class SharedPref {
// Single ton objects...
private static SharedPreferences preference = null;
private static SharedPref sharedPref = null;
//Single ton method for this class...
public static SharedPref getInstance() {
if (sharedPref != null) {
return sharedPref;
} else {
sharedPref = new SharedPref();
return sharedPref;
}
}
/**
* Singleton object for the shared preference.
*
* @param context Context of current state of the application/object
* @return SharedPreferences object is returned.
*/
private SharedPreferences getPreferenceInstance(Context context) {
if (preference != null) {
return preference;
} else {
//TODO: Shared Preference name has to be set....
preference = context.getSharedPreferences("SharedPreferenceName", Context.MODE_PRIVATE);
return preference;
}
}
/**
* Set the String value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value String value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, String value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putString(key, value);
editor.commit();
}
/**
* Set the Integer value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value Integer value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, int value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putInt(key, value);
editor.commit();
}
/**
* Set the boolean value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value Boolean value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, Boolean value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putBoolean(key, value);
editor.commit();
}
/**
* Returns Boolean value for the given key.
* By default it will return "false".
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return false by default; returns the Boolean value for the given key.
*/
public Boolean getBooleanValue(Context context, String key) {
return getPreferenceInstance(context).getBoolean(key, false);
}
/**
* Returns Integer value for the given key.
* By default it will return "-1".
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return -1 by default; returns the Integer value for the given key.
*/
public int getIntValue(Context context, String key) {
return getPreferenceInstance(context).getInt(key, -1);
}
/**
* Returns String value for the given key.
* By default it will return null.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return null by default; returns the String value for the given key.
*/
public String getStringValue(Context context, String key) {
return getPreferenceInstance(context).getString(key, null);
}
}
像这样保存值
SharedPref.getInstance().setSharedValue(this, "Gender1", 1);
像这样检索值,
int gender = SharedPref.getInstance().getIntValue(this, "Gender1");
您可以在您的所有项目中使用此 class。这让您的工作更轻松。
希望这对您有所帮助:)
试试这个。使用静态方法,因此无需创建对象即可访问它
public abstract class AppPref {
private static final String PREF_NAME = "MyPref";
public static void setPreferences(String key, String value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putString(key, value).apply();
}
public static void setPreferences(String key, boolean value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putBoolean(key, value).apply();
}
public static void setPreferences(String key, int value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putInt(key, value).apply();
}
public static String getString(String name, String defaults, Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getString(name, defaults);
}
public static boolean getBoolean(String name, boolean defaults,
Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getBoolean(name, defaults);
}
public static int getInt(String name, int defaults, Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getInt(name, defaults);
}
}
用法
// Set preference
AppPref.setPreferences("key", "value", getApplicationContext());
AppPref.setPreferences("key", 25, getApplicationContext());
AppPref.setPreferences("key", true, getApplicationContext());
// Get preference value
AppPref.getString("key", "default value",getApplicationContext());
AppPref.getInt("key", 0, getApplicationContext());
AppPref.getBoolean("key", false, getApplicationContext());
我知道这个问题已经被问过很多次了,但我不明白如何设置,因为我是 android 的新手。
我试过这个:
1. saved prefrence
2. also use this
这是我的代码:
SharedPreferences.Editor editor = sharedpreferences.edit();
String gender = Gender.get(i).toString();
editor.putString(Gender1, gender);
editor.commit();
list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// int selectedPosition = gender.getSelectedItemPosition();
gender.setSelection(sharedpreferences.getInt("Gender1", -1));
list.indexOf(sharedpreferences.getString(Gender1, "Gender1"));
gender.setAdapter(dataAdapter);
编辑:-
我想在我已经存储在首选项中的微调器中设置存储值。
请帮助我。
我想在用户保存他的帐户详细信息时存储此值
提前致谢。
自行解决:
fgender= sharedpreferences.getString(Gender1, "Gender1");
if(fgender.equals("Female")){
gender.setSelection(1);
}else
{
gender.setSelection(0);
}
再次感谢大家的回答。
这是我的 EasyPref class
public class EasyPref {
public EasyPref(Context context) {
sp = context.getSharedPreferences("com.example", Context.MODE_PRIVATE);
}
public void write(final String key, final String value) {
sp.edit().putString(key, value).commit();
}
public void write(final String key, final boolean value) {
sp.edit().putBoolean(key, value).commit();
}
public void write(final String key, final int value) {
sp.edit().putInt(key, value).commit();
}
public void write(final String key, final Set<String> value) {
sp.edit().putStringSet(key, value).commit();
}
public String read(final String key, final String alt) {
return sp.getString(key, alt);
}
public boolean read(final String key, final boolean alt) {
return sp.getBoolean(key, alt);
}
public int read(final String key, final int alt) {
return sp.getInt(key, alt);
}
public Set<String> read(final String key, final Set<String> alt) {
return sp.getStringSet(key, alt);
}
private SharedPreferences sp;
}
只需输入您案例的关键和价值。
SharedPreferences.Editor editor = getEditor(context);
editor.putString("KEY", "VALUE");
editor.apply();
你可以这样保存
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Gender1", -1);
editor.apply();
并从首选项中检索保存的值
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPreferences.getInt("Gender1", 0);
复制并使用此 classs,通过使用此您可以轻松保存和检索共享首选项值..
/**
* This class handles the all the shared preference operation.
* .i.e., creating shared preference and to set and get value.
*
* @author Jeevanandhan
*/
public class SharedPref {
// Single ton objects...
private static SharedPreferences preference = null;
private static SharedPref sharedPref = null;
//Single ton method for this class...
public static SharedPref getInstance() {
if (sharedPref != null) {
return sharedPref;
} else {
sharedPref = new SharedPref();
return sharedPref;
}
}
/**
* Singleton object for the shared preference.
*
* @param context Context of current state of the application/object
* @return SharedPreferences object is returned.
*/
private SharedPreferences getPreferenceInstance(Context context) {
if (preference != null) {
return preference;
} else {
//TODO: Shared Preference name has to be set....
preference = context.getSharedPreferences("SharedPreferenceName", Context.MODE_PRIVATE);
return preference;
}
}
/**
* Set the String value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value String value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, String value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putString(key, value);
editor.commit();
}
/**
* Set the Integer value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value Integer value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, int value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putInt(key, value);
editor.commit();
}
/**
* Set the boolean value in the shared preference W.R.T the given key.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @param value Boolean value which is to be stored in shared preference.
*/
public void setSharedValue(Context context, String key, Boolean value) {
getPreferenceInstance(context);
Editor editor = preference.edit();
editor.putBoolean(key, value);
editor.commit();
}
/**
* Returns Boolean value for the given key.
* By default it will return "false".
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return false by default; returns the Boolean value for the given key.
*/
public Boolean getBooleanValue(Context context, String key) {
return getPreferenceInstance(context).getBoolean(key, false);
}
/**
* Returns Integer value for the given key.
* By default it will return "-1".
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return -1 by default; returns the Integer value for the given key.
*/
public int getIntValue(Context context, String key) {
return getPreferenceInstance(context).getInt(key, -1);
}
/**
* Returns String value for the given key.
* By default it will return null.
*
* @param context Context of current state of the application/object
* @param key String used as a key for accessing the value.
* @return null by default; returns the String value for the given key.
*/
public String getStringValue(Context context, String key) {
return getPreferenceInstance(context).getString(key, null);
}
}
像这样保存值
SharedPref.getInstance().setSharedValue(this, "Gender1", 1);
像这样检索值,
int gender = SharedPref.getInstance().getIntValue(this, "Gender1");
您可以在您的所有项目中使用此 class。这让您的工作更轻松。
希望这对您有所帮助:)
试试这个。使用静态方法,因此无需创建对象即可访问它
public abstract class AppPref {
private static final String PREF_NAME = "MyPref";
public static void setPreferences(String key, String value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putString(key, value).apply();
}
public static void setPreferences(String key, boolean value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putBoolean(key, value).apply();
}
public static void setPreferences(String key, int value, Context context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit()
.putInt(key, value).apply();
}
public static String getString(String name, String defaults, Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getString(name, defaults);
}
public static boolean getBoolean(String name, boolean defaults,
Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getBoolean(name, defaults);
}
public static int getInt(String name, int defaults, Context context) {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getInt(name, defaults);
}
}
用法
// Set preference
AppPref.setPreferences("key", "value", getApplicationContext());
AppPref.setPreferences("key", 25, getApplicationContext());
AppPref.setPreferences("key", true, getApplicationContext());
// Get preference value
AppPref.getString("key", "default value",getApplicationContext());
AppPref.getInt("key", 0, getApplicationContext());
AppPref.getBoolean("key", false, getApplicationContext());