This is my code. If I click “14 ” then in a spinner should be set as a default value as 14, and if I changed “20” or some values like “16” it should set as a default value in a Spinner.
So selected value must be displayed in a Spinner.
Spinner font=new Spinner(con);
option= new String[] {"Select","14","16","18","20"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,option);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
font.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
if(!(arg0).getSelectedItem().toString().trim().equalsIgnoreCase("Select"))
size=Float.parseFloat((option[(arg0).getSelectedItemPosition()]).trim());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
font.setAdapter(adapter);
int i;
for( i=0;i<option.length;i++){
if(option[i].toString().equalsIgnoreCase("14")){
}
else {
font.setSelection(i);
}
}
When you goto next screen and comeback then it automatically displays the last selected value by default (if your for loop code not exists here). if you goto previous screen from the spinner screen and come back then spinner will be created again and adapter is set again. so value selected last cannot be set. If still that is your requirement then save the value to shared preferences when selected. then after setting adapter, you get the value if already exists. In your code instead of checking equal condition with “14” you check it with shared preference saved value and call setSelection(i) in if loop