public class GeneralSettingsActivity extends Activity {
String[] data = {"9600", "19200", "38400", "57600", "115200"};
Spinner BaudrateSpinner;
int BaudratePosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_settings);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
BaudrateSpinner = (Spinner) findViewById(R.id.BaudrateSpinner);
BaudrateSpinner.setAdapter(adapter);
// Загружаем сохраненую позицию елемента спиннера
SharedPreferences loadbaudrate = getSharedPreferences("BaudrateSavedFile",0);
BaudratePosition = loadbaudrate.getInt("BaudrateSpinnerPosition",0);
// выделяем элемент
BaudrateSpinner.setSelection(BaudratePosition);
// устанавливаем обработчик нажатия
BaudrateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// Сохраняем позицию и значение baudrate
SharedPreferences savebaudrate = getSharedPreferences("BaudrateSavedFile", MODE_PRIVATE);
SharedPreferences.Editor editor = savebaudrate.edit();
editor.putInt("BaudrateSpinnerPosition", position).putString("BaudrateSpinnerText",BaudrateSpinner.getSelectedItem().toString());
editor.apply();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}