gpt4 book ai didi

java - 为什么我使用 result.getInt(2)/result.getInt(3) 和 result.getString(0)

转载 作者:行者123 更新时间:2023-12-02 02:39:26 29 4
gpt4 key购买 nike

我有点困惑为什么在 RoutineRetrieved 函数中分配 ACTIVITYIMAGE 时使用 result.getInt(2) 并在分配 SLOT 时使用 result.getInt(3)...而在 ColourChange 函数中我使用 result。分配 DAY 时使用 getInt(0)。

我之前假设我指向的是 sqlite 数据库中的列。但现在我很困惑。谁能解释一下这些数字的含义?

例程检索函数:

    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);
}
}
}

颜色更改函数:

    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));
}
}
}

数据库.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;
}

最佳答案

方法 getIntgetString 中的数字参数是您想要获取的列的索引,从 0 开始,相对于您的查询已经完成了

您有 2 个不同的查询。在 checkColour() 方法中,您有以下查询,该查询只有一个字段:

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

所以,当你打电话

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

fieldValue 将具有查询的第一个字段的值,在本例中为 DayOfWeek

在另一个查询中也是如此,即表 Routines 的 SELECT *。在本例中,索引指的是 CREATE TABLE 语句中的字段位置。

关于java - 为什么我使用 result.getInt(2)/result.getInt(3) 和 result.getString(0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723070/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com