-3

I know this question has been asked many times but I don't understand how to set up because I am new in android.

I have tried this:

1. saved prefrence

2. also use this

Here is my code:

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

EDIT:-
I want to set stored value in spinner I already stored in Preference.

Please help me.

I want to store in this value when user saves his account detail

Thank you in Advance.

Self Solution:

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

Again, thank you all for answers.

Community
  • 1
  • 1
seggy
  • 1,176
  • 2
  • 18
  • 38

5 Answers5

0

This is my 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;
}
San Droid
  • 332
  • 1
  • 7
  • This is only acceptable if you have to store a few values. But if you have to store a lot of values "at the same time" you should use the answer from @mohamed amine – San Droid Nov 10 '16 at 10:07
0

you can save it like

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("Gender1", -1);
    editor.apply();

And retrieve the saved value from the preferences

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getInt("Gender1", 0);
Anirudh Goud
  • 346
  • 2
  • 11
0

Copry and use this classs, byy using this you can easily save and retrieve the shared preference value..

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

        }

Save the value like this

SharedPref.getInstance().setSharedValue(this, "Gender1", 1);

Retrieve the value like this,

int gender = SharedPref.getInstance().getIntValue(this, "Gender1");

You can use this class in all your project. Which makes your job easier.

Hope this is helpful :)

Jeevanandhan
  • 1,073
  • 10
  • 18
0

Try this. Use static methods, so you can access it without creating object

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

}

Usage

// 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());
Mable John
  • 4,518
  • 3
  • 22
  • 35
  • want to set value in spinner...like Male Female – seggy Nov 10 '16 at 10:19
  • You mean this :- AppPref.setPreferences("Gender", "Male", getApplicationContext()); and AppPref.getString("Gender", "not_saved_yet",getApplicationContext()); – Mable John Nov 10 '16 at 10:24
  • thanks for rply but how to retrive saved value in spinner and set in spinner.. sorry for this because i m new in android and i just lead this project – seggy Nov 10 '16 at 10:26
-1

Just put the key and value of your case.

SharedPreferences.Editor editor = getEditor(context);
        editor.putString("KEY", "VALUE");
        editor.apply();
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96