0

我有一个具有编辑文本的应用程序。它已经有一个默认值一 (1)。然后用户可以更改为他想要更改为 3 的任何内容。然后他可以从微调器中选择一个食物值。每种食物都有相应的卡路里。

例如,我选择一个含有 8 卡路里的面包,然后在编辑文本 2 中输入。总卡路里应该是 16 卡路里。8 * 2。请看我到目前为止尝试过的内容:

     String[] classes = {
    "Cornbread",
    "French Bread",
    "French Toast",
    "French Toast, low fat",
    "Italian Bread",
    "Wheat Bread",
    "Wheat Bread, low calories",
    "Wheat Bread, whole wheat"
    };

String mealname = selected;
String serving = calories.getText().toString();
int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));

String servng = String.valueOf(i);

int amount = Integer.valueOf(etAmount.getText().toString());
int answer  = amount * i;
String strAnswer = String.valueOf(answer);
calories.setText(strAnswer);

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sdf.format(new Date());

if ( ( mealname.isEmpty() || servng.isEmpty() ) ){

    // call for custom toast
    viewErrorToast();
}public void onItemSelected(AdapterView<?> parent, View arg1, int position,
    long arg3) {
// TODO Auto-generated method stub
selected = parent.getItemAtPosition(position).toString();

switch( position ){
case 0:
    strCalories = "188 calories";
    calories.setText(strCalories);
    break;
case 1:
    strCalories = "185 calories";
    calories.setText(strCalories);
    break;
case 2:
    strCalories = "126 calories";
    calories.setText(strCalories);
    break;
case 3:
    strCalories = "149 calories";
    calories.setText(strCalories);
    break;
case 4:
    strCalories = "81 calories";
    calories.setText(strCalories);
    break;
case 5:
    strCalories = "66 calories";
    calories.setText(strCalories);
    break;
case 6:
    strCalories = "46 calories";
    calories.setText(strCalories);
    break;
case 7:
    strCalories = "89 calories";
    calories.setText(strCalories);
    break;
}

}


private void initControls() {
// TODO Auto-generated method stub

// RadioGroup 
rgMeal = (RadioGroup) findViewById (R.id.rgSelectMeal);

sp = (Spinner) findViewById (R.id.spFoodVegetable);
save = (Button) findViewById (R.id.btFoodVegetableSave);
calories = (Button) findViewById (R.id.btFoodVegetableCalories);
back = (Button) findViewById (R.id.tabs_back);
home = (Button) findViewById (R.id.tabs_home);
tv = (TextView) findViewById (R.id.txtMenuHeader);
etAmount = (EditText) findViewById (R.id.etAmount);
tv.setText(R.string.whitebread);

ArrayAdapter<String> array =
        new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, classes);
sp.setAdapter(array);
sp.setOnItemSelectedListener(this);

back.setOnClickListener(this);
home.setOnClickListener(this);
save.setOnClickListener(this);

}                                                           

更新:

问题是,它没有成倍增加。我从按钮中的文本中获取卡路里值,然后将其乘以编辑文本中的用户输入。

4

1 回答 1

0

一个更好的方法是

 String[] classes = {
"Cornbread",
"French Bread",
"French Toast",
"French Toast, low fat",
"Italian Bread",
"Wheat Bread",
"Wheat Bread, low calories",
"Wheat Bread, whole wheat" 
};

//put how much calorie each food item has in this array
int[] calories = {188, 185, 126, 149, 81, 66, 46, 89};

public void onItemSelected(AdapterView<?> parent, View arg1, int position,
long arg3) {
     // TODO Auto-generated method stub
     selected = parent.getItemAtPosition(position).toString();
     int amount = Integer.valueOf(etAmount.getText().toString())
     int total = amount * calories[position];
     strCalories = total + " calories";
     calories.setText(strCalories);

}
于 2013-01-30T11:46:05.190 回答