为了账号安全,请及时绑定邮箱和手机立即绑定

Spinner 无法保存所选值

Spinner 无法保存所选值

MYYA 2021-11-03 16:24:17
在我向你解释之前,我告诉过你,我一直在 stackoverflow 和许多网站中搜索所有问题,所以不要将其标记为重复或任何负面行为。我已经尝试过,努力工作,但仍然卡在这里很多天。我需要你解决一个简单的问题。我有一个关于微调器的问题。我尝试使用共享首选项来保存默认值,它起作用了。但是当我每次都尝试保存选定的微调器值时,它失败了,每当我返回上一页时,我都无法检索我之前选择的值。FontSettings.javapublic class FontSettings extends AppCompatActivity {private Spinner spinner1, spinnerLatin;private SharedPreferences mMyPrefs;private SharedPreferences.Editor mMyEdit;@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.settings_font);    // toolbar    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    setSupportActionBar(toolbar);    //this line shows back button    getSupportActionBar().setDisplayHomeAsUpEnabled(true);    //Display data size teks arab in dropdown list spinner    Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);    //save selected spinner value    SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);    SharedPreferences.Editor editor=sharedPref.edit();    editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());    editor.apply();    //Display data size teks latin in dropdown list spinner    Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);    ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);    spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);    spinnerLatin.setAdapter(spinnerArrayLatin);    // spinnerLatin default value    spinnerLatin.setSelection(1);    addListenerOnSpinnerItemSelection();    addListenerOnSpinner2ItemSelection();}
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

要将所选项目保存在您的 sharedPreferences 中,请在您的onCreate(...) 中使用此代码:


final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);

        spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);

                SharedPreferences.Editor editor=sharedPref.edit();

                editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());

                editor.apply();

            }


            @Override

            public void onNothingSelected(AdapterView<?> parent) {


            }

        });

要始终选择 Spinner 上的上一个选定项目,只需在将数据放入 Spinner 后调用此代码:


 SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);


        final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);


        int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);

        spinnerBackgroundChange.setSelection(lastSelectedPosition);

请记住,您当前正在保存所选项目的位置而不是值。


更新(下面的新答案)


在下面添加另一个解决方案。我注意到您将用于更新位置的代码放置在错误的位置,因此我只是将该代码放置在正确的位置(在您初始化 Spinner 之后)。请尝试一下,如果它不起作用,请告诉我。


    public class FontSettings extends AppCompatActivity {


    private Spinner spinner1, spinnerLatin;

    private SharedPreferences mMyPrefs;

    private SharedPreferences.Editor mMyEdit;


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings_font);


        // toolbar

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        //this line shows back button

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        //Display data size teks arab in dropdown list spinner

        final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);

        ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);

        spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);

        spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);


        // Code pasted here

        if (spinnerBackgroundChange.getSelectedItemPosition() == 0) {

            int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);

            spinnerBackgroundChange.setSelection(lastSelectedPosition);

        }


        spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);

                SharedPreferences.Editor editor=sharedPref.edit();

                editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());

                editor.apply();


               // Code removed here

            }


            @Override

            public void onNothingSelected(AdapterView<?> parent) {


            }

        });



        //Display data size teks latin in dropdown list spinner

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

        ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);

        spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);

        spinnerLatin.setAdapter(spinnerArrayLatin);

        // spinnerLatin default value

        spinnerLatin.setSelection(1);


        addListenerOnSpinnerItemSelection();

        addListenerOnSpinner2ItemSelection();

    }


    public void addListenerOnSpinnerItemSelection() {

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

        spinner1.setOnItemSelectedListener(new SizeArabFont());

    }


    public void addListenerOnSpinner2ItemSelection() {

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

        spinnerLatin.setOnItemSelectedListener(new SizeLatinFont());

    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case android.R.id.home:

                this.finish();

                return true;

        }

        return super.onOptionsItemSelected(item);

    } 

}


查看完整回答
反对 回复 2021-11-03
  • 1 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信