我有一個按鈕(星期幾)的主菜單。如果參考一週中的這些日子將數據存儲在數據庫中,那麼它們將變成「綠色」。我理解SQL查詢本身,但不明白colourChange函數如何標識每個按鈕並'知道'如何更改它。有人可以解釋這是如何工作的?主菜單按鈕改變顏色,但不知道如何
MainMenu.java
private void colourChange() {
Cursor result = myDb.checkColour();
if (result.getCount() == 0) { // If the result equals to 0 then do nothing.
// Default colour remains
} else {
// if the result is not 0 then...
while (result.moveToNext()) { // Move through each result...
String day = result.getString(0); // and store the day (column 0) of the result in day
findViewById(getResources().getIdentifier(day + "button", "id", getPackageName())) // Find the view by ID using getResources.getIdentifier and passing the following parameter (day)
.setBackgroundColor(getResources().getColor(R.color.colorSuccess)); // The variable colourSuccess stored in the colours.xml file sets the background colour green.
}
}
}
Database.java
public Cursor checkColour() { // a SELECT statement is used to SELECT DayOfWeek FROM RoutineTable and GROUP BY DayOfWeek and store this as result.
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek", null);
return result;
}
您是否嘗試過用調試器對其進行檢查並檢查變量? – litelite
你的問題是什麼? – Milk