'How to save the selection of a spinner in a dialog box
I have a dialog box with a spinner in it.
So far, I have managed to code the spinner so that it saves the last known selection whenever I open up the dialog box again containing said spinner.
Albeit, I'm trying to get shared preferences to save the index of the selection and then retrieve it in my activity's onCreate, so that I can attribute a corresponding action to it when the app starts up. I'm having difficulties, in so much that the app is now crashing with an error code with reference to the spinner (referenced as powerSpinnerView throughout my activity) being a 'null object reference'.
Can anybody please advise on where I might be going wrong? If you require additional details, then please let me know - more than happy to provide. Hopefully what I said makes sense!
Code snippet from spinner in Dialog box:
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_options_menu);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
View back = dialog.findViewById(R.id.arrow);
PowerSpinnerView powerSpinnerView = (PowerSpinnerView) dialog.findViewById(R.id.spItems);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
powerSpinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<String>() {
@Override
public void onItemSelected(int oldIndex, @Nullable String oldItem, int newIndex, String newItem) {
if (newIndex == 0) {
// save inputted spinner position to sharedpreferences
int userChoice = powerSpinnerView.getSelectedIndex();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor prefEditor = sharedPreferences.edit();
prefEditor.putInt("userChoiceSpinner", userChoice);
prefEditor.apply();
Toast.makeText(getApplicationContext(),"CURRENCY CHANGED TO USD",Toast.LENGTH_SHORT).show();
Code snippet from OnCreate:
final Dialog dialog = new Dialog(MainActivity.this);
PowerSpinnerView powerSpinnerView = (PowerSpinnerView) dialog.findViewById(R.id.spItems);
// Retrieve spinner position from sharedpreferences
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int spinnerValue = sharedPreferences.getInt("userChoiceSpinner", -1);
if (spinnerValue != -1){
// set the value of the spinner
powerSpinnerView.selectItemByIndex(spinnerValue);
Toast.makeText(getApplicationContext(),"SUCCESS!",Toast.LENGTH_SHORT).show();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|