无法保存微调状态的android [英] can't save spinner state android
问题描述
我尝试过的教程,但我不能挽救我的飞旋的状态。
I tried an tutorial but I cannot save the state of my spinner.
下面是我的code:
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中):
To saved the states (these are after 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");
}
我想保存,即使应用程序被关闭微调状态或活动停止。有人可以解释为什么它不工作。
I am trying to save the spinner state even if the app is closed or the activity stopped. Can someone explain why it's not working.
推荐答案
我得到它的工作使用共享preferences,
I got it working using shared preferences,
-
我得到微调的位置是:
I get the spinner position:
spinnerPos = ARG2;
我创建的设置/初始化里面的onCreate共享preferences:
I created set/initialize shared preferences inside onCreate:
共享preferences spinnersaving = getShared preferences(spinnerstate,0);
使用共享preferences的方法的onStop与微调的位置是:
Use an onStop method for sharedpreferences with the spinner position:
protected void onStop(){
super.onStop();
SharedPreferences spinnersaving = getSharedPreferences("spinnerstate",0);
SharedPreferences.Editor editor = spinnersaving.edit();
editor.putInt("spinnerPos", spinnerPos);
editor.commit();
}
在微调点击监听功能,我从每股preferences微调位置,并将其设置到微调:
before the spinner click listener function I get the spinner position from sharepreferences and set it onto the spinner:
spinner.setSelection(spinnersaving.getInt(spinnerPos,0));
然后它的工作。 :)
这篇关于无法保存微调状态的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!