通过意图传递数据计算

时间:2012-05-12 14:57:58

标签: android android-intent

我遇到了一些问题,我知道如何传递值数据。 例如在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 在一项新活动下?我必须使用意图还是其他方式? 先感谢您。非常感谢你的帮助。 :)

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

    }

}

这是一个活动,另外两个活动只是不同的变量,现在我不知道如何计算一个新活动下的所有结果?谢谢:D

2 个答案:

答案 0 :(得分:0)

我会在您的最终活动中创建一个public static long kaloriResult来保存结果,然后从每个活动中添加。{/ p>

答案 1 :(得分:0)

您还可以创建名为totalKalori的变量,并在每个活动中,您可以将totalKalori发送到另一个活动。从之前的Intent获得totalKalori后,您可以在将totalKalori=+calculateLunch(food,drink)发送到下一个活动

之前致电{{1}}