Hello I want to Know that how can i keep the spinner items selected throughout the activity so that if i come back form other activity to the activity that is consisting of spinner its still remain selected as it is.
The above statement that i have used that has been solved after this there is an another issue that whenever i restart my app its the selected portion remains as it is i want to know what i can do so that the spinner does not show what i have selected before
I have a code of spinner which i want to keep it selected throughout all the activities ,
daySelection = (Spinner)findViewById(R.id.daypreferance);
//String[] dayName = new String[]{
// "sunday", "monday", "tuesday"
//};
MyClass[] dayName ={
new MyClass("sunday", ""),
new MyClass("monday", "2"),
new MyClass("tuesday", "3")
};
ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this , R.layout.spinner_items ,R.id.spinneritem,dayName);
adapter.setDropDownViewResource(R.layout.spinner_items);
hotelSelection.setAdapter(adapter);
daySelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
preferDay= parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void onPause() {
super.onPause();
daySelection = (Spinner)findViewById(R.id.daypreferance);
SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
prefs.edit().putInt("spinner_indx", daySelection.getSelectedItemPosition()).apply();
}
public void onResume() {
super.onResume();
daySelection = (Spinner)findViewById(R.id.daypreferance);
SharedPreferences prefs = getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
int spinnerIndx = prefs.getInt("spinner_indx", 0);
daySelection.setSelection(spinnerIndx);
}
I need to add the code that whenever I restart my app its the selected portion remains as it is I want to know what I can do so that the spinner does not show what I have selected before