简体   繁体   English

如何通过微调器使用共享首选项

[英]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我的活动中有 3 个微调器,每当我打开应用程序时我都需要显示之前选择的微调器值
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.假设我第一次在微调器中选择“CSE”,如果我再次重新打开应用程序,那么默认情况下必须选择“CSE”。
I have used shared_preferences to store the values and tried retrieving them but could't be any use.我已经使用 shared_preferences 来存储值并尝试检索它们但没有任何用处。
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?任何人都可以指导我完成这个吗?

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您可以在此处找到有关共享首选项的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM