 |
ดึงค่าในฟิลด์จาก Database SQLite เพื่อมาวน loop คำนวณเก็บสะสมผลรวมไปเรื่อยๆ ต้องดึงยังไงครับ |
|
 |
|
|
 |
 |
|
ผมได้เก็บข้อมูล จำนวน * kcal แล้วเก็บผลรวมของค่านี้ไว้ตอน insert แล้ว
แต่ที่นี้ผมต้องการเก็บผลรวมทั้งหมดจากที่เก็บข้อมูลไว้ด้านบนให้มัน + สะสมไปเรื่อยๆ ผมจะต้องใช้วิธีไหนครับที่จะสามารถคำนวณได้
- ดึงจากฐานข้อมูลมาทีละตัว แล้วก็ค่อย + สะสมไป หรือว่าให้คำนวณได้จากที่เราเอามาแสดงแล้วใน textview เลยได้ไหมครับ
ช่วยแนะนำทีนะครับ
final ProfileKcalDBClass myProKcalDb = new ProfileKcalDBClass(this);
final ArrayList<HashMap<String, String>> ProfileKcalList = myProKcalDb.SelectAllData();
ListView ListViewShowProKcal = (ListView) findViewById(R.id.listViewShowProKcal);
SimpleAdapter sAdapProKcal;
int[] num = {};
int sum = 0;
for(int i = 0; i < num.length; i++){
sum = sum + num[i];
}
Toast.makeText(getApplicationContext(), "" + sum, Toast.LENGTH_LONG).show();
sAdapProKcal = new SimpleAdapter(MainActivity.this, ProfileKcalList, R.layout.activity_column_profile_kcal,
new String[] {"kcal_pro_name", "kcal_pro_unit", "kcal_pro_total"},
new int[] {R.id.Name, R.id.Unit, R.id.Total});
ListViewShowProKcal.setAdapter(sAdapProKcal);
Tag : Java, Android, JAVA
|
ประวัติการแก้ไข 2015-02-19 11:11:12 2015-02-19 11:15:19 2015-02-19 11:16:33 2015-02-19 11:50:17 2015-02-20 20:52:27 2015-02-20 21:06:17 2015-02-20 21:07:33 2015-02-20 21:08:03 2015-02-20 21:08:19 2015-02-20 21:10:28 2015-02-20 21:10:48 2015-02-20 21:46:38 2015-02-20 21:54:23 2015-02-20 21:54:42 2015-02-20 21:56:24 2015-02-20 21:57:09 2015-02-22 18:55:29 2015-02-22 18:56:23
|
 |
 |
 |
 |
Date :
2015-02-19 11:10:31 |
By :
l3ios |
View :
1903 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำที่ SQLite ก็ได้นี่ครับ
ตัวอย่างการ Loop Database SQLite (Android-Java)
public String[] SelectData(String strMemberID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" },
"MemberID=?",
new String[] { String.valueOf(strMemberID) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
/***
* 0 = MemberID
* 1 = Name
* 2 = Tel
*/
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
|
 |
 |
 |
 |
Date :
2015-02-19 17:48:45 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จัดไป
|
 |
 |
 |
 |
Date :
2015-02-20 07:54:17 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|