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));
}
java.lang.ClassCastException:java.lang.Integer中不能轉換爲java.lang.String錯誤在那第二行提取 – DaxHR
究竟應該在if(selectedText!= null){?我不明白 – DaxHR
將代碼粘貼到您設置微調器適配器的位置。 –