我有一个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);
}
}
答案 0 :(得分:1)
您需要获得列结果,如下所示
@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);
}