2017-08-16 37 views
0

我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; 
} 
+1

它指向一個[零基於columnIndex](https://developer.android.com/reference/android/database/Cursor.html#getString(INT)),爲什麼'字符串天= result.getString(0) ;'指向主鍵'Routine'我不能告訴(應用程序是在主鍵可能實現),但我個人不會使用columnIndex以'SELECT *'聲明,因爲它是足夠您更改數據庫和你的代碼會中斷。 –

回答

2

數量參數的方法中getIntgetString是索引,由0開始,你想得到的列,相對於你所做的查詢

你有2個不同的查詢。在方法checkColour(),您有以下查詢,其中只有一個領域:

"SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek" 

所以,當你調用

Cursor result = myDb.retrieveRoutine(currentDay);  
String fieldValue = result.getString(0); 

fieldValue將有查詢的第一個字段的值,在這種情況下,DayOfWeek

在另一個查詢中,即表例程的SELECT *相同。在這種情況下,索引是指CREATE TABLE聲明中的字段位置。

+0

那麼你是說用CheckColour查詢它是0,因爲它只返回一個字段?雖然RetrieveRoutine查詢指的載於在Database.java文件的頂部CREATE TABLE語句的順序 - 換句話說DAYOFWEEK(第一),活動(第二)和插槽(第三)。 – MarkW

+0

差不多!其實第一場'Routine',第二個是'DayOfWeek',等(: – mustache1up

+0

謝謝mustache1up! – MarkW