如何在共享首选项中保存值并在微调器中设置

时间:2016-11-10 09:52:55

标签: android sharedpreferences 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);

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

请帮帮我。

我希望在用户保存其帐户详细信息时存储此值

先谢谢你。

自我解决方案:

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

再次,谢谢大家的答案。

5 个答案:

答案 0 :(得分:0)

这是我的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;
}

答案 1 :(得分:0)

你可以像

一样保存它
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);

答案 2 :(得分:0)

复制并使用此类,通过使用此类,您可以轻松保存和检索共享首选项值。

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

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

希望这有用:)

答案 3 :(得分:0)

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

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

答案 4 :(得分:-1)

只需记下案件的关键和价值。

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