У меня есть эти два класса.SettingsManager
расширяет другой класс и сохраняет данные, а также получает контекст из Activity в качестве параметра.Однако это не похоже на работу, и я получаю пустые EditText
поля.Не могли бы вы предложить что-нибудь, чтобы это исправить?
public class SettingsActivity extends Activity {
private EditText _userSoftSerialNumberEditText;
private EditText _databaseServerEditText;
private EditText _databaseNameEditText;
private EditText _userApplicationEditText;
private EditText _databaseUserNameEditText;
private EditText _databasePasswordEditText;
private SettingsManager _settingsManager;
private Context _context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settingsview_layout);
_context = this;
_settingsManager = new SettingsManager(_context);
_settingsManager.loadSettings();
// Setting the values in the EditText fields in the settingsview_layout.xml
_userSoftSerialNumberEditText = (EditText) findViewById(R.id.userSoftSerialNumberEditText);
_userSoftSerialNumberEditText.setText(_settingsManager.getUserSoftSerialNumber());
_databaseServerEditText = (EditText) findViewById(R.id.databaseServerEditText);
_databaseServerEditText.setText(_settingsManager.getDatabaseServer());
_databaseNameEditText = (EditText) findViewById(R.id.databaseNameEditText);
_databaseNameEditText.setText(_settingsManager.getDatabaseName());
_userApplicationEditText = (EditText) findViewById(R.id.userApplicationEditText);
_userApplicationEditText.setText(_settingsManager.getUserApplication());
_databaseUserNameEditText = (EditText) findViewById(R.id.databaseUserNameEditText);
_databaseUserNameEditText.setText(_settingsManager.getDatabaseUserName());
_databasePasswordEditText = (EditText) findViewById(R.id.databasePasswordEditText);
_databasePasswordEditText.setText(_settingsManager.getDatabasePassword());
// Creating a "Save Settings" button
Button _saveSettingsButton = (Button) findViewById(R.id.saveSettingsButton);
// Implementing the "Save Settings" button click
_saveSettingsButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
_settingsManager.saveSettings();
}
});
}
}
public class SettingsManager extends ConnectionInfo {
private SharedPreferences _settings;
private Context _context;
public SettingsManager (Context context) {
_context = context;
_settings = PreferenceManager.getDefaultSharedPreferences(context);
}
public void saveSettings() {
SharedPreferences.Editor _editor = _settings.edit();
_editor.putString("userSoftSerialNumber", _userSoftSerialNumber);
_editor.putString("databaseUserName", _databaseUserName);
_editor.putString("databasePassword", _databasePassword);
_editor.putString("databaseServer", _databaseServer);
_editor.putString("databaseName", _databaseName);
_editor.putString("userApplication", _userApplication);
_editor.commit(); // Never forget the commit()!!!!
}
public void loadSettings() {
_userSoftSerialNumber = _settings.getString("userSoftSerialNumber", "");
_databaseUserName = _settings.getString("databaseUserName", "");
_databasePassword = _settings.getString("databasePassword", "");
_databaseServer = _settings.getString("databaseServer", "");
_databaseName = _settings.getString("databaseName", "");
_userApplication = _settings.getString("userApplication", "");
}
}