我尝试了一个教程,但我无法保存我的微调器的状态。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_main);
//language selection list
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.language_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// storing string resources into Array
lang_list = getResources().getStringArray(R.array.language_list);
Toast.makeText(getBaseContext(), "You have selected : " + lang_list[index],
Toast.LENGTH_SHORT).show();
choice = spinner.getSelectedItem().toString();
spinnerPos = arg2;
final ImageView country_flag = (ImageView)findViewById(R.id.country);
String s=((TextView)arg1).getText().toString();
if(s.equals("English"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.eng_spinner));
if(s.equals("German"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.german_spinner));
if(s.equals("French"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.french_spinner));
if(s.equals("Spanish"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.spanish_spinner));
}
/*private int getPersistedItem() {
String keyName = makePersistedItemKeyName();
return PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getInt(keyName, 0);
}
protected void setPersistedItem(int position) {
String keyName = makePersistedItemKeyName();
PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit().putInt(keyName, position).commit();
}
private String makePersistedItemKeyName() {
return currentUserName + "_your_key";
}*/
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
}
}
保存状态(这些是在oncreate之后):
protected void onResume(){
super.onResume();
spinner.setSelection(spinnerPos);
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("spinnerIndex", spinnerPos);
Log.d("SpinnerState", "Spinner at position " + spinnerPos + " was saved");
}
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getInt("spinnerIndex");
Log.d("SpinnerState", "Spinner at position " + spinnerPos + " was restored");
}
我正在尝试保存微调器状态,即使应用程序已关闭或活动已停止。有人可以解释为什么它不起作用。
答案 0 :(得分:1)
我使用共享偏好设置
我得到了微调位置:
spinnerPos = arg2;
我在onCreate:
中创建了set / initialize共享首选项 SharedPreferences spinnersaving = getSharedPreferences("spinnerstate",0);
对spinner位置使用onStop方法进行共享偏好:
protected void onStop(){
super.onStop();
SharedPreferences spinnersaving = getSharedPreferences("spinnerstate",0);
SharedPreferences.Editor editor = spinnersaving.edit();
editor.putInt("spinnerPos", spinnerPos);
editor.commit();
}
我从sharepreferences获取微调器位置并将其设置到微调器上:
spinner.setSelection(spinnersaving.getInt("spinnerPos", 0));
然后它奏效了。 :)
答案 1 :(得分:0)
onRestoreInstanceState你必须为spinnerPos分配保存的值,如下所示:
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
spinnerPos = savedInstanceState.getInt("spinnerIndex");
Log.d("SpinnerState", "Spinner at position " + spinnerPos + " was restored");}