如何将微调器保存到已保存/共享的首选项

时间:2016-02-24 14:16:27

标签: android sharedpreferences spinner

我可以在保存的首选项中保存字符串,但很难保存我的微调器。

 public class Diet extends Activity {

  private SharedPreferences sharedPreferences;
  Spinner spnCalorieRange;

这是我的onCreate:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);

name = loadSavedPreference("name");
strAge = loadSavedPreference("strAge");
strHeight = loadSavedPreference("strHeight");
strWeight = loadSavedPreference("strWeight");

etName.setText(name);
etAge.setText(strAge);
etHeight.setText(strHeight);
etWeight.setText(strWeight);

这是我在onCreate中的Spinner:

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

        }

    });

这是在我的onCLick后面的一个按钮,我在那里保存字符串和微调器

 age = (int) Double.parseDouble(etAge.getText().toString());
        height = (int) Double.parseDouble(etHeight.getText().toString());
        weight = (int) Double.parseDouble(etWeight.getText().toString());


        //Save Preferences
        String strAge = Integer.toString(age);
        String strHeight = Integer.toString(height);
        String strWeight = Integer.toString(weight);

        name = etName.getText().toString();
        savePreference("name",name);

        strAge = etAge.getText().toString();
        savePreference("strAge",strAge);

        strHeight = etHeight.getText().toString();
        savePreference("strHeight",strHeight);

        strWeight = etWeight.getText().toString();
        savePreference("strWeight",strWeight);:

2 个答案:

答案 0 :(得分:0)

您无法将实际的微调器对象保存到共享的首选项,但是您可以保存使微调器工作的所有值,并且下次创建微调器时,只需应用这些值

答案 1 :(得分:0)

onItemSelected地方:

int spinnerPosition = spinner.getSelectedItemPosition();
saveSpinnerPosition(spinnerPosition);

保存方法

public void saveSpinnerPosition(int position){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    prefEditor.putInt("spnCalorieRange",position); 
    prefEditor.apply();
}

加载方法

public void loadSpinnerPosition{
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    int position= sharedPreferences .getInt"spnCalorieRange",-1);
    if(spinnerValue > -1)

    // set the value of the spinner 
    spinner.setSelection(position);
}

要将微调器放回原位,请在loadSpinnerPosition

中拨打onCreate

<强> -------------------------------------------- -------------------------------------------------- --------------------------------------

修改

因为我注意到this post也是您的问题,您也可以在一次时执行所有操作:

在您的活动的顶部

int spinnerPosition; 

onItemSelected地方:

spinnerPosition = spinner.getSelectedItemPosition();

保存方法

public void saveMethod(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("Gender", radioSexButton.isChecked());
    editor.putBoolean("Male", rdoMale.isChecked());
    prefEditor.putInt("spnCalorieRange",spinnerPosition); 
    prefEditor.apply();
}    

加载方法

public void loadMethod(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    int position= sharedPreferences .getInt"spnCalorieRange",-1);
    radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false)); 
    rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));

    if(spinnerValue > -1)

    // set the value of the spinner 
    spinner.setSelection(position);
}

然后当您要保存所有状态时调用saveMethod(),并在您要加载所有状态时致电loadMethod()

应该帮助你