[英]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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.