如何将Spinner保存为共享首选项?

时间:2016-02-13 14:03:48

标签: android sharedpreferences spinner

我能够将整数和字符串保存为共享首选项,但是已经搜索过,似乎无法将Spinners选择的值保存为共享首选项?

        spinner = (Spinner)findViewById(R.id.spnCalorieRange); 
        adapter = ArrayAdapter.createFromResource(this, R.array.Calorie_Range, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                long item = parent.getItemIdAtPosition(position);
                String pos =spinner.getSelectedItem().toString();
                //sharedPreferences.edit().putInt("PREF_SPINNER", position).commit();
                if (item == 0){
                    deficitPercentage = .05;
                }
                else if (item ==1)
                {
                    deficitPercentage = .1;
                }
                else if (item ==2)
                {
                    deficitPercentage = .15;    
                }
                else if (item ==3)
                {
                    deficitPercentage = .2;
                }
                else if (item ==4)
                {
                    deficitPercentage = .25;
                }
                else
                {
                    deficitPercentage = .3;
                }
                editor.putString("pos", pos);
                editor.commit();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

在OnCreate中访问共享首选项:

       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_diet);


        spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);

        sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences pref = getApplicationContext().getSharedPreferences("Options", MODE_PRIVATE);
        editor=pref.edit();

         String strAvgCalIntake = Double.toString(dailyCalorieIntake);
         String strGoal = Double.toString(goal);

        strAvgCalIntake = loadSavedPreference("strAvgCalIntake");
        strGoal = loadSavedPreference("strGoal");

        etAverageCalorieIntake.setText(strAvgCalIntake);
        etLoseWeight.setText(strGoal);

        //mPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        //spinner.setSelection(mPrefs.getInt(PREF_SPINNER, 0));

        // int selectedPosition = sharedpreferences.getInt("spinnerSelection", 0);


        int selectedPosition = spnCalorieRange.getSelectedItemPosition();
        sharedPreferences.getInt("spinnerSelection", selectedPosition);
        ((Editor) sharedPreferences).commit();

以下是我保存共享偏好设置的按钮: 字符串正在节省下来。

  Button btnBack = (Button)findViewById(R.id.btnBack);
    btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View v) {

                     String strAvgCalIntake =       Double.toString(dailyCalorieIntake);
                    String strGoal = loadSavedPreference("strGoal");
                spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);

                    strAvgCalIntake = etAverageCalorieIntake.getText().toString();
                    savePreference("strAvgCalIntake",strAvgCalIntake);

                strGoal = etLoseWeight.getText().toString();
                savePreference("strGoal",strGoal);

                    SharedPreferences spref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = spref.edit();
                editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
                    editor.commit();

1 个答案:

答案 0 :(得分:0)

如果您已验证deficitPercentage值,(我希望您这样做)

SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
editor.commit();