0

我的问题是按钮无法传递 int 数据,这是
我想在 TotalKalori 和 bmr 之间进行比较的代码,但似乎 TotalKalori 在上一节中无法解决,它说 TotalKalori 无法解析为变量,我该如何解决这?谢谢

package lynn.calculate.KaunterKalori;

 import lynn.calculate.KaunterKalori.R;
 import android.app.Activity;
//import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;

public class CalculateAll extends Activity implements OnClickListener {

//private int TotalKalori;
protected int totalKalori1;
protected int totalKalori2;
protected int totalKalori3;

EditText EstimateCalorie;
TextView TotalKaloriSehari;
TextView totalsarapan;
TextView totallunch;
TextView totaldinner;
TextView calorieneeds;
TextView resultDiff;


public CalculateAll() {

}

@Override
public void onCreate(Bundle savedInstancesState) {
    super.onCreate(savedInstancesState);
    setContentView(R.layout.calculate_all);

    View kiraButton = findViewById(R.id.buttonKiraAll);
    kiraButton.setOnClickListener(this);


    View bezaButton = findViewById(R.id.caloriediff);
    bezaButton.setOnClickListener(this);
    //

    TotalKaloriSehari = (TextView) findViewById(R.id.JumlahKalori);
    totalsarapan = (TextView) findViewById(R.id.sarapantext);
    totallunch = (TextView) findViewById(R.id.lunchtext);
    totaldinner = (TextView) findViewById(R.id.dinnertext);
    EstimateCalorie = (EditText) findViewById(R.id.BMR);
    calorieneeds = (TextView) findViewById(R.id.BMR);
    resultDiff = (TextView) findViewById(R.id.result);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        totalKalori1 = extras.getInt("totalBreakfast");
        totalKalori2 = extras.getInt("totalLunch");
        totalKalori3 = extras.getInt("totalDinner");

    }

    totalsarapan.setText(totalKalori1 + "");
    totallunch.setText(totalKalori2 + "");
    totaldinner.setText(totalKalori3 + "");




}

public void onClick(View v) {
    if (v.getId() == R.id.buttonKiraAll) {


     int TotalKalori = calculateTotalKalori(totalKalori1, totalKalori2,
                totalKalori3);
        TotalKaloriSehari.setText(TotalKalori + "");



    }

    if (v.getId()== R.id.caloriediff) {

         int TotalKalori = calculateTotalKalori(totalKalori1, totalKalori2,
                    totalKalori3);

        int nilaikalori = Integer.parseInt(EstimateCalorie.getText()
                .toString());
        //TotalKalori = Integer.parseInt(TotalKaloriSehari.getText().toString());

        int bmr = calculatebmr(nilaikalori);


    //  String deskripsiKalori = describekalori(bmr);


    //  resultDiff.setText("kalori makanan" + TotalKalori + " calori diperlukan " + bmr + "=" + deskripsiKalori);
        resultDiff.setText("kalori makanan = " + TotalKalori + " calori diperlukan = " + bmr);
        }
    } 
        //Intent n = new Intent(this, MainActivity.class);
        //startActivity(n);



        //







private String describekalori(int bmr) {

 if (bmr < TotalKalori ) {
        return "bykkan makan";
    } else if (bmr == TotalKalori) {

        return "kekalkan jumlah kalori ini";

    } else if (bmr > TotalKalori) {

        return "kurangkan pengambilan kalori";

    } else
        return "oiii";

}       




public int calculateTotalKalori(int totalKalori1, int totalKalori2,
        int totalKalori3) {
    return (int) (totalKalori1 + totalKalori2 + totalKalori3);

}

public int calculatebmr(int nilaikalori) {
    return (int) (nilaikalori);
}


}

// the comparison that I can't solve is the 
// the TotalKalori cannot resolved into variable? how do I solve this? thank you
private String describekalori(int bmr) {

 if (bmr < TotalKalori ) {
        return "bykkan makan";
    } else if (bmr == TotalKalori) {

        return "kekalkan jumlah kalori ini";

    } else if (bmr > TotalKalori) {

        return "kurangkan pengambilan kalori";

    } else
        return "oiii";

}
4

1 回答 1

0

我认为您想在按下卡路里差异按钮时将bmr传递给MainActivity 。在按钮按下时使用以下代码传递数据:

    Intent n = new Intent(this, MainActivity.class);  
    Bundle bundle = new Bundle();
    bundle.putString(“myValue“,""+bmr);
    n.putExtras(bundle);
    startActivity(n);

现在在您的MainActivity中从包中检索您的数据:

    Bundle bundle = getIntent().getExtras();
    String retbmr = bundle.getString(“myValue”); 

如果需要,然后解析为整数:

int nbmr = Integer.parseInt(retbmr);

或直接作为:

int nbmr = Integer.parseInt(bundle.getString(“myValue”));
于 2012-05-24T00:52:23.953 回答