猿问

Spinner 无法保存所选值

在我向你解释之前,我告诉过你,我一直在 stackoverflow 和许多网站中搜索所有问题,所以不要将其标记为重复或任何负面行为。我已经尝试过,努力工作,但仍然卡在这里很多天。我需要你解决一个简单的问题。


我有一个关于微调器的问题。我尝试使用共享首选项来保存默认值,它起作用了。但是当我每次都尝试保存选定的微调器值时,它失败了,每当我返回上一页时,我都无法检索我之前选择的值。


FontSettings.java


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

    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();

}


MYYA
浏览 133回答 1
1回答

慕尼黑5688855

要将所选项目保存在您的 sharedPreferences 中,请在您的onCreate(...) 中使用此代码:final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);&nbsp; &nbsp; &nbsp; &nbsp; spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor editor=sharedPref.edit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.apply();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });要始终选择 Spinner 上的上一个选定项目,只需在将数据放入 Spinner 后调用此代码:&nbsp;SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);&nbsp; &nbsp; &nbsp; &nbsp; int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);&nbsp; &nbsp; &nbsp; &nbsp; spinnerBackgroundChange.setSelection(lastSelectedPosition);请记住,您当前正在保存所选项目的位置而不是值。更新(下面的新答案)在下面添加另一个解决方案。我注意到您将用于更新位置的代码放置在错误的位置,因此我只是将该代码放置在正确的位置(在您初始化 Spinner 之后)。请尝试一下,如果它不起作用,请告诉我。&nbsp; &nbsp; public class FontSettings extends AppCompatActivity {&nbsp; &nbsp; private Spinner spinner1, spinnerLatin;&nbsp; &nbsp; private SharedPreferences mMyPrefs;&nbsp; &nbsp; private SharedPreferences.Editor mMyEdit;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.settings_font);&nbsp; &nbsp; &nbsp; &nbsp; // toolbar&nbsp; &nbsp; &nbsp; &nbsp; Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);&nbsp; &nbsp; &nbsp; &nbsp; setSupportActionBar(toolbar);&nbsp; &nbsp; &nbsp; &nbsp; //this line shows back button&nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setDisplayHomeAsUpEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; //Display data size teks arab in dropdown list spinner&nbsp; &nbsp; &nbsp; &nbsp; final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);&nbsp; &nbsp; &nbsp; &nbsp; spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);&nbsp; &nbsp; &nbsp; &nbsp; spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);&nbsp; &nbsp; &nbsp; &nbsp; // Code pasted here&nbsp; &nbsp; &nbsp; &nbsp; if (spinnerBackgroundChange.getSelectedItemPosition() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerBackgroundChange.setSelection(lastSelectedPosition);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor editor=sharedPref.edit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.apply();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Code removed here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; //Display data size teks latin in dropdown list spinner&nbsp; &nbsp; &nbsp; &nbsp; Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);&nbsp; &nbsp; &nbsp; &nbsp; spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);&nbsp; &nbsp; &nbsp; &nbsp; spinnerLatin.setAdapter(spinnerArrayLatin);&nbsp; &nbsp; &nbsp; &nbsp; // spinnerLatin default value&nbsp; &nbsp; &nbsp; &nbsp; spinnerLatin.setSelection(1);&nbsp; &nbsp; &nbsp; &nbsp; addListenerOnSpinnerItemSelection();&nbsp; &nbsp; &nbsp; &nbsp; addListenerOnSpinner2ItemSelection();&nbsp; &nbsp; }&nbsp; &nbsp; public void addListenerOnSpinnerItemSelection() {&nbsp; &nbsp; &nbsp; &nbsp; spinner1 = (Spinner) findViewById(R.id.spinner1);&nbsp; &nbsp; &nbsp; &nbsp; spinner1.setOnItemSelectedListener(new SizeArabFont());&nbsp; &nbsp; }&nbsp; &nbsp; public void addListenerOnSpinner2ItemSelection() {&nbsp; &nbsp; &nbsp; &nbsp; spinnerLatin = (Spinner) findViewById(R.id.spinnerLatin);&nbsp; &nbsp; &nbsp; &nbsp; spinnerLatin.setOnItemSelectedListener(new SizeLatinFont());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onOptionsItemSelected(MenuItem item) {&nbsp; &nbsp; &nbsp; &nbsp; switch (item.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case android.R.id.home:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return super.onOptionsItemSelected(item);&nbsp; &nbsp; }&nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答