我在活动中有简单的微调器。如果我使用sharedPreferences选择列表中的内容,我想保存微调器的位置。但是,当我换到另一个活动并再次回来时,它会重置为默认状态。我不知道为什么,我的偏好没有保存,或者我的偏好没有加载。
这是我的活动
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
/**
* Created by Bani Burhanuddin on 25/02/2016.
*/
public class Intent6 extends AppCompatActivity {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.halaman_6);
Button btnback = (Button) findViewById(R.id.buttonPrev6);
Button btnHome = (Button) findViewById(R.id.buttonHome6);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent6.this, Main.class));
finish();
}
});
btnback.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent6.this, Intent5.class));
finish();
}
});
final Spinner spinner6 = (Spinner) findViewById(R.id.spinner6);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.android_layout_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner6.setAdapter(adapter);
final String firstItem = String.valueOf(spinner6.getSelectedItem());
spinner6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (firstItem.equals(String.valueOf(spinner6.getSelectedItem()))) {
} else {
Toast.makeText(parent.getContext(),
"Ko Milih : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SharedPreferences test = getSharedPreferences("Preferences6_1", Context.MODE_PRIVATE);
int spinnerValue = test.getInt("Preferences6_1",-1);
if(spinnerValue != -1)
// set the value of the spinner
spinner6.setSelection(spinnerValue);
}
@Override
public void onPause() {
super.onPause();
Spinner spinner6 = (Spinner) findViewById(R.id.spinner6);
SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences6", 0).edit();
prefEditor.putInt("spinner6", spinner6.getSelectedItemPosition());
prefEditor.apply();
}
}
答案 0 :(得分:0)
您在阅读和写作时使用不同的共享偏好名称。你用来阅读的关键也是错误的。
请更改以下代码
SharedPreferences test = getSharedPreferences("Preferences6_1", Context.MODE_PRIVATE);
int spinnerValue = test.getInt("Preferences6_1",-1);
到
SharedPreferences test = getSharedPreferences("Preferences6", Context.MODE_PRIVATE);
int spinnerValue = test.getInt("spinner6",-1);