从TextView的光标求和值

时间:2016-02-04 12:22:02

标签: android cursor

我正在尝试对两个游标中的值求和,并将结果放入文本视图中。当光标只返回一个值时,它可以完美地运行,但是当返回的值超过一个时,我必须将光标放入while循环中,它将停止工作。

主要的游标是ingredientsCursor返回一个或多个列表(这是有效的),然后在while循环中,两个游标从主游标中获取结果以执行两个单独的查询,然后将结果相乘并添加到要显示的运行总计中。

当我在没有while循环的情况下运行代码时,知道我只会从ingredientsCursor获得一个结果,一切正常,当有多个结果时我必须使用while循环。我已经将总数设置为一个全局变量,如果出现问题但仍然只是获得空白区域。

使用结果显示工作显示(无静止循环)

        final Cursor ingredientsCursor = adapter.getRecipesIngredients(recipeCode);
        ingredientsCursor.moveToFirst();

        String name = ingredientsCursor.getString(ingredientsCursor.getColumnIndex("ingredient_name"));

        final Cursor c = adapter.getRecipeCalories(name);
        c.moveToFirst();
        int cal = c.getInt(c.getColumnIndex("calories"));

        final Cursor m = adapter.getMeasurement(recipeCode, name);
        m.moveToFirst();
        int meas = m.getInt(m.getColumnIndex("measurement"));

        int total = cal * meas;

        recipeCalories.setText("Calories:" + total);

使用While循环,显示空白

    final Cursor ingredientsCursor = adapter.getRecipesIngredients(recipeCode);
    ingredientsCursor.moveToFirst();
    while(ingredientsCursor.moveToNext())
    {
        String name = ingredientsCursor.getString(ingredientsCursor.getColumnIndex("ingredient_name"));

        final Cursor c = adapter.getRecipeCalories(name);
        c.moveToFirst();
        int cal = c.getInt(c.getColumnIndex("calories"));

        final Cursor m = adapter.getMeasurement(recipeCode, name);
        m.moveToFirst();
        int meas = m.getInt(m.getColumnIndex("measurement"));

        int MainActivity.total = cal * meas;

    }

    recipeCalories.setText("Calories:" + MainActivity.total);

2 个答案:

答案 0 :(得分:0)

每次while循环运行时,都会覆盖MainActivity.total。 尝试将所有总值放在值列表中并显示数据。

答案 1 :(得分:0)

您将覆盖每个循环步骤的总值。

  final Cursor ingredientsCursor = adapter.getRecipesIngredients(recipeCode);
    ingredientsCursor.moveToFirst();
    while(ingredientsCursor.moveToNext())
    {
        String name = ingredientsCursor.getString(ingredientsCursor.getColumnIndex("ingredient_name"));

        final Cursor c = adapter.getRecipeCalories(name);
        c.moveToFirst();
        int cal = c.getInt(c.getColumnIndex("calories"));

        final Cursor m = adapter.getMeasurement(recipeCode, name);
        m.moveToFirst();
        int meas = m.getInt(m.getColumnIndex("measurement"));
        // here is your error
        int MainActivity.total = cal * meas;

    }

您需要将代码更改为:

   final Cursor ingredientsCursor = adapter.getRecipesIngredients(recipeCode);
    ingredientsCursor.moveToFirst();
    int sum = 0;
    while(ingredientsCursor.moveToNext())
    {
        String name = ingredientsCursor.getString(ingredientsCursor.getColumnIndex("ingredient_name"));

        final Cursor c = adapter.getRecipeCalories(name);
        c.moveToFirst();
        int cal = c.getInt(c.getColumnIndex("calories"));

        final Cursor m = adapter.getMeasurement(recipeCode, name);
        m.moveToFirst();
        int meas = m.getInt(m.getColumnIndex("measurement"));

        sum += (cal * meas);

    }

    recipeCalories.setText("Calories:" + sum);