値データを渡す方法を知っていたところに問題があります。たとえば、Activity1 : メソッドを使用しています
int kalorivalue1 = calculateBreakfast(food1, drink1);
public int calculateBreakfast(int food1, int drink1) {
return (int) (food1 + drink1);
アクティビティ 2 で
int kalorivalue2 = calculateLunch(food2 , drink2);
public int calculateLunch(int food2, int drink2) {
return (int) (food2 + drink2);
アクティビティ 3 で >
int kaloriValue3 = calculateDinner(food3, drink3);
public int calculateDinner(int food3, int drink3) {
return (int) (food3 + drink3);
問題は、すべての = kaloriValue1 + kaloriValue2 + kaloriValue3 を 1 つの NEW アクティビティでどのように計算するかです。インテントを使用する必要がありますか、それとも他の方法ですか? 前もって感謝します。あなたの助けは大歓迎です。:)
package lynn.calculate.KaunterKalori;
import lynn.calculate.KaunterKalori.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
//import android.content.Intent;
public class Breakfast extends Activity {
public int totalKalori;
ArrayAdapter<String> FoodType1Adapter;
ArrayAdapter<String> DrinkType1Adapter;
String FoodTypeArray[] = { "nasi lemak", "roti canai",
"Roti(2kpg-White Bread)", "nasi kerabu", "tosei", "nasi goreng" };
int[] valueFoodTypeArray = { 389, 301, 140, 20, 1, 637 };
String[] DrinkTypeArray = { "teh panas", "teh 0 panas", "teh tarik" };
int[] valueDrinkTypeArray = { 201, 101, 51 };
Spinner FoodTypeSpinner;
Spinner DrinkTypeSpinner;
TextView SarapanTotalKalori;
public void onCreate(Bundle savedInstancesState) {
super.onCreate(savedInstancesState);
setContentView(R.layout.breakfast);
FoodTypeSpinner = (Spinner) findViewById(R.id.spinner1);
DrinkTypeSpinner = (Spinner) findViewById(R.id.spinner2);
SarapanTotalKalori = (TextView) findViewById(R.id.JumlahKalori);
initializeSpinnerAdapters();
// load the default values for the spinners
loadFoodValue1Range();
loadDrinkValue1Range();
}
// nk handle button --> refer calculate button
public void calculateClickHandler(View view) {
if (view.getId() == R.id.button1) {
// nk bace dkat spinner
int food1 = getSelectedFood();
int drink1 = getSelectedDrink();
// kira kalori sarapan
// view kalori sarapan
int kaloriValue1 = calculateSarapan(food1, drink1);
SarapanTotalKalori.setText(kaloriValue1 + "");
totalKalori = kaloriValue1;
// Bundle extras = getIntent().getExtras();
//if (extras != null) {
// int sarapanKalori = extras.getInt("kaloriValue1");
//setCurrentSarapanKalori(kaloriValue1);
//CurrentSarapanKalori();
}
}
//Intent i= new Intent(this, CalculateAll.class);
//i.putExtra("sarapanKalori", kaloriValue1);
//}
public int getSelectedFood() {
String selectedFoodValue = (String) FoodTypeSpinner.getSelectedItem();
int index = 0;
for (int i = 0; i < FoodTypeArray.length; i++) {
if (selectedFoodValue.equals(FoodTypeArray[i])) {
index = i;
break;
}
}
return valueFoodTypeArray[index];
}
public int getSelectedDrink() {
String selectedDrinkValue = (String) DrinkTypeSpinner.getSelectedItem();
int index = 0;
for (int i = 0; i < DrinkTypeArray.length; i++) {
if (selectedDrinkValue.equals(DrinkTypeArray[i])) {
index = i;
break;
}
}
return valueDrinkTypeArray[index];
}
public int calculateSarapan(int food1, int drink1) {
return (int) (food1 + drink1);
}
public void loadFoodValue1Range() {
FoodTypeSpinner.setAdapter(FoodType1Adapter);
// set makanan b4 pilih
FoodTypeSpinner.setSelection(FoodType1Adapter.getPosition("400"));
}
public void loadDrinkValue1Range() {
DrinkTypeSpinner.setAdapter(DrinkType1Adapter);
DrinkTypeSpinner.setSelection(DrinkType1Adapter.getPosition("77"));
}
public void initializeSpinnerAdapters() {
FoodType1Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, FoodTypeArray);
DrinkType1Adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, DrinkTypeArray);
}
}
これはアクティビティの 1 つで、他の 2 つのアクティビティは変数が異なるだけです。1 つの新しいアクティビティですべての結果を計算する方法がわかりませんでした。ありがとう