【问题标题】:how to Save value in Shared Preference and set in spinner如何在共享首选项中保存值并在微调器中设置
【发布时间】:2016-11-14 22:30:08
【问题描述】:

我知道这个问题已经被问过很多次了,但我不明白如何设置,因为我是 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);

编辑:-
我想在我已经存储在 Preference 中的微调器中设置存储值。

请帮帮我。

当用户保存他的帐户详细信息时,我想存储在这个值中

在此先感谢您。

自行解决:

fgender= sharedpreferences.getString(Gender1, "Gender1");
    if(fgender.equals("Female")){
        gender.setSelection(1);
    }else
    {
        gender.setSelection(0);
    }

再次感谢大家的回答。

【问题讨论】:

    标签: android sharedpreferences spinner


    【解决方案1】:

    你可以保存它

    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);
    

    【讨论】:

    • 在偏好中保存后...如何在微调器中设置
    • 返回一个整数值来区分黑白男女,然后将选定的pos值设置为它
    • int selectionPosition="YOUR_VALUE"; spinner.setSelection(selectionPosition);
    【解决方案2】:

    这是我的 EasyPref 类

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

    【讨论】:

    • 这仅在您必须存储一些值时才可接受。但是如果你必须“同时”存储很多值,你应该使用@mohamed amine 的答案
    【解决方案3】:

    复制并使用这个类,通过使用它,您可以轻松保存和检索共享偏好值..

    /**
     * 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");
    

    你可以在你的所有项目中使用这个类。这让您的工作更轻松。

    希望这有帮助:)

    【讨论】:

    • @seggy:您可以点击此链接在微调器中设置值。
    • @seggy:抱歉忘记在上面的评论中粘贴链接。给你stackoverflow.com/a/4228121
    【解决方案4】:

    试试这个。使用静态方法,这样你就可以在不创建对象的情况下访问它

        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());
    

    【讨论】:

    • 想在微调器中设置值...比如男性女性
    • 你的意思是:- AppPref.setPreferences("Gender", "Male", getApplicationContext());和 AppPref.getString("Gender", "not_saved_yet",getApplicationContext());
    • 感谢 rply,但如何在微调器中检索保存的值并在微调器中设置.. 对此感到抱歉,因为我是 android 新手,我只是领导这个项目
    【解决方案5】:

    只要把你的案子的关键和价值。

    SharedPreferences.Editor editor = getEditor(context);
            editor.putString("KEY", "VALUE");
            editor.apply();
    

    【讨论】:

    • 感谢 rply 我已经这样做了,但是在检索该数据时出现问题
    • 好吧,把 putString 改成 getString 就行了。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多