0

这是我创建的功能。Playgolf 表有列 id、holepar、strokes、putts。我正在寻找总结笔画。

我已经编辑了代码,现在我收到了这个错误......有什么想法吗?

public void DisplaySum(int sum)
{
    //Setup a local variable to store sum
    int intRoundsSUM = 0;
    db.open(); 
    //Setup your database cursor
    Cursor c = db.getPLAYGOLF();
    Toast.makeText(getBaseContext(), intRoundsSUM, Toast.LENGTH_LONG).show();

    //Iterate through the cursor
    //Sum up each value
   c.moveToFirst();
        while(c.isAfterLast() == false) {
            intRoundsSUM += c.getInt(2);
            c.moveToNext();
        }
    Toast.makeText(getBaseContext(), intRoundsSUM, Toast.LENGTH_LONG).show();
}
    // LOG CAT MAIN ERROR: 
    //E/AndroidRuntime(597): android.content.res.Resources$NotFoundException: String resource ID #0xa
4

4 回答 4

1

你必须管理你的光标,从

cursor.moveToFirst();
while(cursor.isAfterLast() == false) {
    intRoundsSUM += c.getInt(2);
    cursor.moveToNext();
}

另请阅读有关让您的活动控制光标的生命周期的信息activity.startManagingCursor(cursor);

于 2012-04-26T14:02:34.873 回答
1

您可以使用下一个循环遍历游标行:

while (cursor.moveToNext()) {
    intRoundsSUM += c.getInt(2);
}

但您也可以尝试从数据库中进行“求和”选择。像这样的东西:

SELECT SUM(strokes_column) FROM golf_table

它将返回一列一行存储必要总和的表。

于 2012-04-26T14:05:46.150 回答
0

尝试修复错误c.moveToFirst()intRoundsSUM += c.getInt(2);

总结它们所有你可以使用while (c.moveToNext())或在你的 sql 查询中求和(这会更好)。见http://www.w3schools.com/sql/sql_func_sum.asp

于 2012-04-26T14:13:18.533 回答
-1

可能有两个原因:

  1. Playgolf 桌子是空的。通过使用检查大小c.getCount();
  2. 您在光标的末尾。用于c.moveToFirst()转到光标的开头。
于 2012-04-26T14:17:56.157 回答