我abit困惑爲什麼在RoutineRetrieved函數中我使用result.getInt(2)時分配ACTIVITYIMAGE和result.getInt(3)當在ColourChange函數中分配SLOT ....時,我在分配DAY時使用了result.getInt(0)。爲什麼我使用result.getInt(2)/ result.getInt(3)和result.getString(0)
我以前認爲我指着我的SQLite數據庫列。但現在我很困惑。有人能解釋這些數字是什麼意思嗎?
RoutineRetrieved功能:
private void routineRetrieved() {
Cursor result = myDb.retrieveRoutine(currentDay);
if (result.getCount() == 0) {
// Do nothing
} else {
while (result.moveToNext()) {
int ActivityImage = result.getInt(2);
int Slot = result.getInt(3);
ImageView emptySlot = (ImageView) findViewById(Slot);
emptySlot.setImageResource(ActivityImage);
}
}
}
ColourChange功能:
private void colourChange() {
Cursor result = myDb.checkColour();
if (result.getCount() == 0) {
// Default colour remains
} else {
while (result.moveToNext()) {
String day = result.getString(0);
findViewById(getResources().getIdentifier(day + "button", "id", getPackageName()))
.setBackgroundColor(getResources().getColor(R.color.colorSuccess));
}
}
}
Database.java
public class Database extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "application.db";
public static final int DATABASE_VERSION = 9;
// Table Name
public static final String RoutineTable = "Routines";
// Column Names
public static final String RoutineColumn1 = "DayOfWeek";
public static final String RoutineColumn2 = "Activity";
public static final String RoutineColumn3 = "Slot";
public Database(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE `Routines` (`Routine` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,`DayOfWeek` TEXT NOT NULL,`Activity` INTEGER NOT NULL, `Slot` INTEGER NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
onCreate(db);
}
public Cursor retrieveRoutine(String selectedDay) { WHERE DayOfWeek equals selectedDay and store this as result.
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + RoutineTable + " WHERE DayOfWeek = '" + selectedDay + "'", null);
return result;
}
public boolean insertRoutine(int activityImage, String selectedDay, int activitySlot) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(RoutineColumn1,selectedDay);
contentValues.put(RoutineColumn2,activityImage);
contentValues.put(RoutineColumn3,activitySlot);
long result = db.insert(RoutineTable, null, contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor checkColour() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek", null);
return result;
}
它指向一個[零基於columnIndex](https://developer.android.com/reference/android/database/Cursor.html#getString(INT)),爲什麼'字符串天= result.getString(0) ;'指向主鍵'Routine'我不能告訴(應用程序是在主鍵可能實現),但我個人不會使用columnIndex以'SELECT *'聲明,因爲它是足夠您更改數據庫和你的代碼會中斷。 –