我在代码中所做的是,当我打开活动并即将从微调器加载值时,我首先从数据库中读取我想要的表。在我的表中,我设置了布尔值,整个表中只有 1 个值是真的。首先,我将默认值设置为 true。如果用户想要更改它,那么当您打开活动并进入默认值时,将您要保存的新值设置为微调器,按保存或您拥有的任何东西,然后在 db 表中更改布尔值新的默认值。此外,当您打开活动时,将 true 的值也设置为微调器中的值。希望这会有所帮助,我并没有感到困惑:P
这是我的代码,也许不是最好的书面代码,但它工作正常:
db.open();
Cursor c1 = db.getAllCurrencies();
currentCurrencyList = new ArrayList<String>();
if (c1.moveToFirst()) {
do {
currentCurrencyList.add(c1.getString(c1
.getColumnIndex(DBAdapter.MY_CURRENT_CURRENCY_NAMES)));
} while (c1.moveToNext());
}
db.close();
dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, currentCurrencyList);
dataAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
currencySpinner.setAdapter(dataAdapter1);
currencySpinner.setSelection(0);
db.open();
Cursor c2 = db.getCurrentCurrency();
currentCurrencyList2 = new ArrayList<String>();
String currencySymbol = "";
if (c2.getCount() == 1) {
currencySymbol = c2.getString(c2
.getColumnIndex(DBAdapter.MY_CURRENT_CURRENCY_NAMES));
}
currentCurrency.setText(currencySymbol);
db.close();
setNewCurrencyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String spinnerSelection = "";
spinnerSelection = currencySpinner.getSelectedItem().toString();
db.open();
db.updateOldCurrency();
db.updateCurrentCurrency(spinnerSelection);
Cursor c3 = db.getCurrentCurrency();
currentCurrencyList3 = new ArrayList<String>();
String currencySymbol2 = "";
if (c3.moveToFirst()) {
do {
currentCurrencyList3.add(c3.getString(c3
.getColumnIndex(DBAdapter.MY_CURRENT_CURRENCY_NAMES)));
} while (c3.moveToNext());
for (int i = 0; i <= currentCurrencyList3.size() - 1; i++) {
currencySymbol2 = currentCurrencyList3.get(i);
}
}
currentCurrency.setText(currencySymbol2);
db.close();
}
});
dpAdapter 类
public boolean updateOldCurrency() {
ContentValues args = new ContentValues();
args.put(MY_CURRENT_CURRENCY_BOOLEAN, 0);
return db.update(MY_CURRENT_CURRENCY_TABLE, args,
MY_CURRENT_CURRENCY_BOOLEAN + "='" + 1 + "'", null) > 0;
}
public boolean updateCurrentCurrency(String rowId) {
ContentValues args = new ContentValues();
args.put(MY_CURRENT_CURRENCY_BOOLEAN, 1);
return db.update(MY_CURRENT_CURRENCY_TABLE, args,
MY_CURRENT_CURRENCY_NAMES + "='" + rowId + "'", null) > 0;
}