获取 SQlite 列的 SUM 值并将其分配给 Textview

Getting a SUM value for a SQlite column and assigning it to Textview

我有一个 Android 应用程序要求用户输入距离作为字段。它被设置为 SQLite REAL 数据类型,因为我需要一个小数位。我想对列求和并在片段中将其显示给用户作为他们覆盖的总距离。我有一个带有以下代码的 DBmanager。我不确定这是否是获取 SUM 值的正确方法。

public Cursor Distance() {
        Cursor Distance = database.rawQuery("SELECT Sum(" + DatabaseHelper.DSNM + ") AS myTotal FROM " + DatabaseHelper.TABLE_NAME, null);
        return Distance;
}

我想在我具有以下代码的 textView 中显示 SUM 值。有人能告诉我哪里出错了吗?谢谢!

public class Summary extends Activity {
    private TextView tnmView;
    private DBManager dbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("Total Distance");
        setContentView(R.layout.fragment_summary);
        tnmView = (TextView) findViewById(R.id.tnmView);
        dbManager = new DBManager(this);
        dbManager.open();
        Cursor Distance = dbManager.Distance();
        tnmView.setText(nm);

    }
}

您需要得到如下列结果

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Total Distance");
    setContentView(R.layout.fragment_summary);
    tnmView = (TextView) findViewById(R.id.tnmView);
    dbManager = new DBManager(this);
    dbManager.open();
    Cursor Distance = dbManager.Distance();

    String result = "";

    // get column value
    if (Distance.moveToNext())
        result = String.valueOf(Distance.getDouble(Distance.getColumnIndex("myTotal")));

    tnmView.setText(result);

}