Spinner不保存为共享/保存的首选项

时间:2016-02-26 23:41:04

标签: android sharedpreferences spinner android-spinner

使用共享首选项来保存字符串,但很难保存我的微调器。

public class MainActivity extends Activity {

Spinner spnExample;
Button btnSave;
ArrayAdapter<CharSequence> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnSave = (Button) findViewById(R.id.btnSave);

    SharedPreferences prefs;

    loadData();

    spnExample = (Spinner) findViewById(R.id.spnExample);
    adapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnExample.setAdapter(adapter);

    spnExample.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

这是我在一个按钮(在onCreate中)保存我的偏好的地方

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences.Editor editor = getPreferences(0).edit();

            EditText etName = (EditText) findViewById(R.id.etName);
            editor.putString("editTextCallIdtext", etName.getText().toString());


            int selectedPosition = spnExample.getSelectedItemPosition();
            editor.putInt("spinnerSelection", selectedPosition);


            editor.commit();

        }
    });
}

这是我用来加载首选项的方法(在onCreate之外)

private void loadData() {

    SharedPreferences prefs = getPreferences(0);

    EditText etName = (EditText) findViewById(R.id.etName);
    String etNametextrestored = prefs.getString("editTextCallIdtext", null);

    spnExample.setSelection(prefs.getInt("spinnerSelection",0));

    etName.setText(etNametextrestored, EditText.BufferType.EDITABLE);
}

1 个答案:

答案 0 :(得分:1)

建议您在loadData初始化后移动spnExample,如下面的代码所示。或者您也可以将loadData移至onResume()

SharedPreferences prefs;

spnExample = (Spinner) findViewById(R.id.spnExample);
adapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnExample.setAdapter(adapter);
spnExample.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

loadData(); // Do this after spnExample got initialized