0

我需要计算根据添加的食物显示的卡路里总量。我的设计是这样的:

Breakfast

[EDITTEXT]

[BUTTON][TEXTVIEW] <--display food name and calories

[BUTTON2][TEXTVIEW2]

[BUTTON3][TEXTVIEW3]

[BUTTON4][TEXTVIEW4]

[BUTTON5][TEXTVIEW5]

午餐和晚餐重复同样的事情

最后,我想要一个 TEXTVIEW 来显示基于添加食物的卡路里总量。

有人知道该怎么做吗?

这是我的代码:

public class MyFoodCalIntakeActivity extends Activity {

private Button breakfastSearchButton;
private Button breakfastSearchButton2;
private Button breakfastSearchButton3;
private Button breakfastSearchButton4;
private Button breakfastSearchButton5;

private Button lunchSearchButton;
private Button lunchSearchButton2;
private Button lunchSearchButton3;
private Button lunchSearchButton4;
private Button lunchSearchButton5;

private Button dinnerSearchButton;
private Button dinnerSearchButton2;
private Button dinnerSearchButton3;
private Button dinnerSearchButton4;
private Button dinnerSearchButton5;

private EditText breakfastEditText;
private EditText lunchEditText;
private EditText dinnerEditText;

private TextView breakfastTextView1;
private TextView breakfastTextView2;
private TextView breakfastTextView3;
private TextView breakfastTextView4;
private TextView breakfastTextView5;

private TextView lunchTextView1;
private TextView lunchTextView2;
private TextView lunchTextView3;
private TextView lunchTextView4;
private TextView lunchTextView5;

private TextView dinnerTextView1;
private TextView dinnerTextView2;
private TextView dinnerTextView3;
private TextView dinnerTextView4;
private TextView dinnerTextView5;

private DatabaseHelper dbHelper = new DatabaseHelper(MyFoodCalIntakeActivity.this,"food",2);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food_calorie_intake);

    。。。

class breakfastSearchButtonListener implements OnClickListener, android.view.View.OnClickListener
{
public void onClick(View v) 
{
    if (v.getId() == R.id.breakfastSearchButton) {

    String display = "";
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    /** the result will be loaded in cursor **/
    Cursor cursor = db.query("food_table", new String[]{"ID","name","calorie"}, null, null, null, null, null);

    while(cursor.moveToNext())
    {

        String name = cursor.getString(cursor.getColumnIndex("name"));
        String calorie = cursor.getString(cursor.getColumnIndex("calorie"));


        if (name.compareTo(breakfastEditText.getText().toString())==0){
            display = display +"Name: "+name+", Calorie: "+calorie;
            breakfastTextView1.setText(display);
        }
    }
}
}

public void onClick(DialogInterface dialog, int which) 
{
    // TODO Auto-generated method stub  
}

} // end activity

请耐心等待,代码有点长。

4

2 回答 2

0

使用运行总计。

有一个变量 int “卡路里”

每当您添加食物时,将它们添加到卡路里并显示

每当您移除食物时,将它们从卡路里中移除并显示

于 2012-08-08T16:14:17.860 回答
0

好吧,您已经有一个保存卡路里值的字符串。您可以通过执行将其转换为 int Integer.parseInt(calorie)。所以只要保持这样的运行总数:

int calorieTotal = 0;
while(cursor.moveToNext())
{

    String name = cursor.getString(cursor.getColumnIndex("name"));
    String calorie = cursor.getString(cursor.getColumnIndex("calorie"));
    calorieTotal += Integer.parseInt(calorie);

    if (name.compareTo(breakfastEditText.getText().toString())==0){
        display = display +"Name: "+name+", Calorie: "+calorie;
        breakfastTextView1.setText(display);
    }
}
<textView>.setText(String.valueOf(calorieTotal));
于 2012-08-08T16:46:41.603 回答