Search code examples
androidspinnerpreferencesshared

Selected Value of Spinner not displayed at textview after going back to the same page again


I faced the issue of not having the textview retaining the value that is selected from the spinner values list. After navigating to the same page, it just keep going back to the same value instead of the value the user has selected. Here is the code that i have written. Thank You.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);

    final Switch mySwitch = (Switch) findViewById(R.id.switchNot);
    final Spinner mySpin = (Spinner) findViewById(R.id.spinNot);
    final TextView tvNot = (TextView) findViewById(R.id.tvTime);

    mySwitch.setOnClickListener(new View.OnClickListener() {
        SharedPreferences.Editor editor = getSharedPreferences("mapp.com.sg.sadtrial", MODE_PRIVATE).edit();
        @Override
        public void onClick(View v) {

            if (mySwitch.isChecked()) {

                editor.putBoolean("Switch", true);
                editor.commit();
                editor.putBoolean("Spinner",true);
                editor.commit();
                mySpin.setEnabled(true);

            } else {

                editor.putBoolean("Switch", false);
                editor.commit();
                editor.putBoolean("Spinner",false);
                editor.commit();
                mySpin.setEnabled(false);
            }
        }
    });

  final SharedPreferences sharedPrefs = 
  getSharedPreferences("mapp.com.sg.sadtrial", MODE_PRIVATE);
  mySwitch.setChecked(sharedPrefs.getBoolean("Switch", false));
  mySpin.setEnabled(sharedPrefs.getBoolean("Spinner",false));

        mySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            SharedPreferences.Editor editor = getSharedPreferences("mapp.com.sg.sadtrial", MODE_PRIVATE).edit();
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch(position){
                    case 0:
                        tvNot.setText(mySpin.getSelectedItem().toString());
                        editor.putString("Option", mySpin.getSelectedItem().toString());
                        editor.commit();
                        break;
                    case 1:
                        tvNot.setText(mySpin.getSelectedItem().toString());
                        editor.putString("Option", mySpin.getSelectedItem().toString());
                        editor.commit();
                        break;
                    case 2:
                        tvNot.setText(mySpin.getSelectedItem().toString());
                        editor.putString("Option", mySpin.getSelectedItem().toString());
                        editor.commit();
                        break;
                    case 3:
                        tvNot.setText(mySpin.getSelectedItem().toString());
                        editor.putString("Option", mySpin.getSelectedItem().toString());
                        editor.commit();
                        break;
                }

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent){
            }

        });

              tvNot.setText(sharedPrefs.getString("Option", mySpin.getSelectedItem().toString()));

}

enter image description here enter image description here

The picture of the left is the value displayed in the textview after user has selected from spinner.

The picture on the right shows the value at textview returning to default and is not retaining user's choice


Solution

  • You are trying to set in textView but every time when you come it will try to set the default value 0 in spinner, So we should get the value and we can try to set that like below

    Instead of the below line

    tvNot.setText(sharedPrefs.getString("Option", mySpin.getSelectedItem().toString()));
    

    Change like this.

    String selectedValue = sharedPrefs.getString("Option", 
    mySpin.getSelectedItem().toString());
    
    if (!TextUtils.isEmpty(selectedValue)) {
        for (int i = 0; i < mySpin.getAdapter().getCount(); i++) {
            String value = (String) mySpin.getAdapter().getItem(i);
    
            if (selectedValue.equalsIgnoreCase(value)) {
                mySpin.setSelection(i);
                break;
            }
        }
    }