[英]Getting a SUM value for a SQlite column and assigning it to Textview
I have an Android app that asks users to enter distance as a field. 我有一个Android应用,要求用户输入距离作为字段。 It is set as a SQLite REAL data type as I need a decimal place.
由于我需要将小数位设置为SQLite REAL数据类型。 I want to sum the column and display that to the user in a fragment as the Total distance they covered.
我想对列求和并将其显示为一个片段,作为用户覆盖的总距离。 I have a DBmanager with the code below.
我有一个带有以下代码的DBmanager。 I'm not sure if that is the correct way to get a SUM value.
我不确定这是否是获取SUM值的正确方法。
public Cursor Distance() {
Cursor Distance = database.rawQuery("SELECT Sum(" + DatabaseHelper.DSNM + ") AS myTotal FROM " + DatabaseHelper.TABLE_NAME, null);
return Distance;
}
I want to display the SUM value in a textView for which I have the code below. 我想在有下面代码的textView中显示SUM值。 Could someone tell me where i'm going wrong?
有人可以告诉我我要去哪里错吗? Thanks!
谢谢!
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);
}
}
You need to get column result as below 您需要获得列结果如下
@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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.