2016-11-10 15 views
-3

この質問は何度も尋ねられていますが、私はアンドロイドで新しくなっているためセットアップ方法がわかりません。共有優先度の値を保存してスピナーに設定する方法

私はこれを試してみました:

ここ

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

EDIT: -
私はスピナーに格納されている値を設定したいです私はすでに嗜好に保存されています。

私を助けてください。私は、ユーザーが

自分のアカウントの詳細を保存するときに、この値に格納する

は事前にありがとうございます。

セルフソリューション:再度

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

優先度を節約しました...スピナーに設定する方法 – seggy

+0

整数値を返してb/w男性と女性を区別し、選択したpos値を –

0

これは

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アミン –

-1

はちょうどあなたの例のキーと値を入れて、私のEasyPrefクラスです。

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

thanxの答えを使用する必要があります。私はこのデータを取得している間にこの問題を解決しました。 – seggy

+0

よくgetStringでputStringを変更してください。 –

0

Copryから保存された値を取得し、あなたが簡単に保存することができ、これを使用してbyy、この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); 
      } 

     } 

あなたはすべてのプロジェクトでこのクラスを使用することができます

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

、このような値を取得し、この

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

のような値を保存します。あなたの仕事が楽になります。

希望します。

+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

スピナーで値を設定したい... like男性女性 – seggy

+0

あなたはこれを意味します: - AppPref.setPreferences( "Gender"、 "Male"、getApplicationContext());およびAppPref.getString( "Gender"、 "not_saved_yet"、getApplicationContext()); – young

+0

rplyのためにありがとうが、スピナーで保存された値を取得し、スピナーに設定する方法。ありがとう、これは私がアンドロイドで新しいと私はちょうどこのプロジェクトをリードするので – seggy

関連する問題