2016-04-06 69 views
0

我有一個Android應用程序,要求用戶輸入距離作爲字段。它被設置爲一個SQLite REAL數據類型,因爲我需要一個小數位。我想對列進行求和並將其顯示給用戶,並將它們作爲他們覆蓋的總距離。我有一個DBmanager,下面的代碼。我不確定這是否是獲取SUM值的正確方法。獲取SQlite列的SUM值並將其分配給Textview

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

我想顯示SUM值在一個textView中,我有下面的代碼。有人能告訴我我要去哪裏嗎?謝謝!

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); 

    } 
} 

回答

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); 

} 
+0

真棒!那完美的作品。謝謝! :-) – MarcusRey

相關問題