2016-11-10 49 views
-3

我知道這個問題已被問了很多次,但我不明白如何設置,因爲我是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); 
    } 

再次感謝大家的答案。

回答

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

ok thanx我會試試.... – seggy

+0

優先保存後...如何在spinner中設置 – seggy

+0

返回一個整數值來區分黑白男女,然後將所選pos值設置爲 –

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

感謝名單我會嘗試這種解決方案訪問它... – seggy

+0

這隻有在您需要存儲幾個值時纔可以接受。但是,如果您必須同時存儲大量值,則應該使用@mohamed amine –

-1

只要把你的案子的關鍵和價值。

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

thanx來回答rply,我有這個問題,但檢索數據 – seggy

+0

時出現問題,只需使用getString更改putString。 –

0

Copry保存值並使用這個類裏面,BYY利用這一點,你可以很容易地保存和檢索共享偏好值。

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

可以在所有項目中使用這個類。這使您的工作更輕鬆。

希望這有幫助:)

+0

如何在微調器中設置值..... – seggy

+0

@seggy:您可以按照此鏈接在微調器中設置值。 – Jeevanandhan

+0

@seggy:對不起,忘了在上面的評論中粘貼鏈接。在這裏你去http://stackoverflow.com/a/4228121 – Jeevanandhan

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

想要設置值在微調...像男性 – seggy

+0

你的意思是這樣的: - AppPref.setPreferences(「Gender」,「Male」,getApplicationContext());和AppPref.getString(「Gender」,「not_saved_yet」,getApplicationContext()); – young

+0

感謝rply,但如何檢索微調器中保存的值,並設置在微調..對不起,因爲我新的Android和我只是領導這個項目 – seggy

相關問題