保存微调器下次启动的值

时间:2017-05-05 11:12:35

标签: android spinner

我想问一下,我们如何存储微调器的值,以便用户在下次保存最后一个选择时访问活动。

1 个答案:

答案 0 :(得分:0)

试试这个

//if use in activity then remove context
    Spinner spinner =(Spinner) findViewById(R.id.spinner);
            SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
            final SharedPreferences.Editor editor = sharedpreferences.edit();
            int position = sharedpreferences.getInt("lastSelectedPosition",0);
            spinner.setSelection(position);

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    editor.putInt("lastSelectedPosition", position);
                    editor.commit();
                }

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

                }
            });