Search code examples
javaandroidsharedpreferencesspinner

How to use shared preferences through spinner


I have 3 spinners in my activity and i need to show the previously selected spinner value whenever i open the application
Suppose if i select "CSE" in my spinner for the first time and if i reopen the app again then "CSE" must be selected by default.
I have used shared_preferences to store the values and tried retrieving them but could't be any use.
Or shall i use a checkbox to remember user data maybe?

   private Button btn;
   private Spinner spin1,spin2,spin3;
   private String sp1,sp2,sp3;
   private CheckBox rememberme;
   private SharedPreferences sharedPreferences;
   private SharedPreferences.Editor editor;
   private boolean savelogin;


   String[] years = {"1", "2", "3", "4"};
   String[] branches = {"CSE", "ECE", "IT", "CIVIL", "EEE", "MECH"};
   String[] sections = {"A", "B", "C","D"};

   @RequiresApi(api = Build.VERSION_CODES.KITKAT)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_sui);


       spin1 =findViewById(R.id.spinner1);
       spin1.setOnItemSelectedListener(this);
       ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);
       //Drop down layout style - list view with radio button
       dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       //attaching dataAdapter to spinner
       spin1.setAdapter(dataAdapter);

       spin2 = findViewById(R.id.spinner2);
       spin2.setOnItemSelectedListener(this);
       ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, branches);
       dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spin2.setAdapter(dataAdapter2);

       spin3 =findViewById(R.id.spinner3);
       spin3.setOnItemSelectedListener(this);
       ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, sections);
       dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spin3.setAdapter(dataAdapter3);

       btn =findViewById(R.id.display);
       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                   displaycheck();
           }});

       sharedPreferences=getSharedPreferences("settings1",MODE_PRIVATE);
       sharedPreferences=getSharedPreferences("settings2",MODE_PRIVATE);
       sharedPreferences=getSharedPreferences("settings3",MODE_PRIVATE);
       int settings1 = sharedPreferences.getInt("spinnerSelection1",0);
       int settings2 = sharedPreferences.getInt("spinnerSelection2",0);
       int settings3 = sharedPreferences.getInt("spinnerSelection3",0);

   }

   public void displaycheck() {
       String sp1 = spin1.getSelectedItem().toString();
       String sp2 = spin2.getSelectedItem().toString();
       String sp3 = spin3.getSelectedItem().toString();

       if (sp1.equals("3") && sp2.equals("CSE") && sp3.equals("B")) {
           Intent it1 = new Intent(this, M2.class);
           startActivity(it1);
}
}

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       //on selecting a spinner item
       String item = parent.getItemAtPosition(position).toString();
       Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

       try {
           int selectedposition1 = spin1.getSelectedItemPosition();
           int selectedposition2 = spin2.getSelectedItemPosition();
           int selectedposition3 = spin3.getSelectedItemPosition();
           editor.putInt("spinnerSelection1",selectedposition1);
           editor.putInt("spinnerSelection2",selectedposition2);
           editor.putInt("spinnerSelection3",selectedposition3);
           editor.apply();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

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

}

Anything that i am missing?
Any reference links that i have to look into?
can anyone guide me through this?


Solution

  • You're retrieving the values from Shared Preferences but you haven't used them to set the selected positions:

    // Get your reference to Shared Preferences
    // (You only need to do this once to store & retrieve your variables from the same file)
    sharedPreferences = getSharedPreferences("My_Shared_Prefs", MODE_PRIVATE);
    
    // Retrieve values from Shared Preferences with a fallback to your default value
    int settings1 = sharedPreferences.getInt("spinnerSelection1",0);
    int settings2 = sharedPreferences.getInt("spinnerSelection2",0);
    int settings3 = sharedPreferences.getInt("spinnerSelection3",0);
    
    // Set the selected position for each Spinner
    spin1.setSelection(settings1);
    spin2.setSelection(settings2);
    spin3.setSelection(settings3);
    

    You also need to initialize your editor variable before you can store your values:

    // Get a reference to the Shared Preferences Editor
    editor = sharedPreferences.edit();
    
    // Store your values
    editor.putInt("spinnerSelection1",selectedposition1);
    editor.putInt("spinnerSelection2",selectedposition2);
    editor.putInt("spinnerSelection3",selectedposition3);
    editor.apply(); // Submit your changes to the Shared Preferences
    

    You can find more info on Shared Preferences here