2016-03-16 79 views
0

所以我有一個微調其設定值到另一個與共享偏好活動的TextView。微調SharedPreferences返回0/1,而不是文本

整個過程的工作,但,代替從旋轉項保存文本,這樣可以節省項目位置(Spinner包含2個可能的值,以便將其保存爲0或1)。

這裏的微調

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

的代碼和第2活動

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

EDIT 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)); 
     } 

回答

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)); 
    } 
+0

java.lang.ClassCastException:java.lang.Integer中不能轉換爲java.lang.String錯誤在那第二行提取 – DaxHR

+0

究竟應該在if(selectedText!= null){?我不明白 – DaxHR

+0

將代碼粘貼到您設置微調器適配器的位置。 –