本文介绍了微调框未保存为共享/保存的首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用共享的首选项来保存String,但是很难保存我的微调器.
Using shared prefernences to save String but having difficulty saving my spinner.
public class MainActivity extends Activity {
Spinner spnExample;
Button btnSave;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSave = (Button) findViewById(R.id.btnSave);
SharedPreferences prefs;
loadData();
spnExample = (Spinner) findViewById(R.id.spnExample);
adapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnExample.setAdapter(adapter);
spnExample.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
在这里,我将首选项保存在按钮后面(在onCreate中)
Here is where I am saving my preferences behind a button (in onCreate)
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getPreferences(0).edit();
EditText etName = (EditText) findViewById(R.id.etName);
editor.putString("editTextCallIdtext", etName.getText().toString());
int selectedPosition = spnExample.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.commit();
}
});
}
这是我用来加载偏好设置的方法(在onCreate之外)
This the method I am using to load the preferences (outside onCreate)
private void loadData() {
SharedPreferences prefs = getPreferences(0);
EditText etName = (EditText) findViewById(R.id.etName);
String etNametextrestored = prefs.getString("editTextCallIdtext", null);
spnExample.setSelection(prefs.getInt("spinnerSelection",0));
etName.setText(etNametextrestored, EditText.BufferType.EDITABLE);
}
推荐答案
建议您在spnExample
初始化后移动loadData
,如下面的代码所示.或者,您也可以将loadData
移至onResume()
Suggest you move the loadData
after spnExample
got initialized as show in the code below. Or you could also move your loadData
to onResume()
SharedPreferences prefs;
spnExample = (Spinner) findViewById(R.id.spnExample);
adapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnExample.setAdapter(adapter);
spnExample.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
loadData(); // Do this after spnExample got initialized
这篇关于微调框未保存为共享/保存的首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!