Spinner SharedPreferences返回0/1而不是Text

时间:2016-03-16 20:03:17

标签: java android spinner android-sharedpreferences

所以我有一个Spinner,它将值设置为另一个带有共享首选项的TextView活动。

整个过程正在运行,但不是保存微调器项目中的文本,而是保存项目位置(Spinner有2个可能的值,因此可以保存0或1)。

这里是Spinner的代码

cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                settings.edit().putInt("PREF_SPINNER", position).commit();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        vrijednostSpinneraCilj = cilj.getSelectedItem().toString();

spremiRezultat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
 cilj.setSelection(settings.getInt("PREF_SPINNER", 0));
                editor.commit();

第二项活动

 ciljTreningRezultat = (TextView) findViewById(R.id.textViewTreningRezultatCilj);
        ciljTreningRezultat.setText(Integer.toString(settings.getInt("PREF_SPINNER", 0)));

我觉得我没有犯过大错。谁能解决这个问题?

修改

  ArrayAdapter<CharSequence> ciljSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj, android.R.layout.simple_spinner_item);
        ciljSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cilj.setAdapter(ciljSpinnerAdapter);
        final Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);

        cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                settings.edit().putString("PREF_SPINNER", cilj.getItemAtPosition(position).toString()).commit();

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

            }
        });
        vrijednostSpinneraCilj = cilj.getSelectedItem().toString();

编辑2

  ciljTreningRezultat = (TextView) findViewById(R.id.textViewTreningRezultatCilj);
        ciljSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj, android.R.layout.simple_spinner_item);
        String selectedText = settings.getString("PREF_SPINNER", null);
        if(selectedText != null){
            cilj.setSelection(ciljSpinnerAdapter.getPosition(selectedText));
        }

1 个答案:

答案 0 :(得分:0)

您没有将所选文本保存到首选项中,而是保存所选项目的位置。您需要像这样保存所选文本 -

@Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                settings.edit().putString("PREF_SPINNER", cilj.getItemAtPosition(position).toString()).commit();
        }

要获取 -

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String selectedText = settings.getString("PREF_SPINNER", null);
if(selectedText != null){
//Get the index of the selectedText from your spinner adapter array and set that position here

cilj.setSelection(ciljSpinnerAdapter.getPosition(selectedText));
    }