-1

My app allows the user to generate a calorie value (main activity 2) and pass that to the main activity so that it may be displayed, when trying to add multiple meals it does not add the previous value and the new value passed with the intent and display it in the main activity, why does it do this? any help would be appreciated, explanations more so.

Main Activity

public class MainActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView calories = (TextView)findViewById(R.id.overall);
        String previouscalories = calories.getText().toString();
        int numold = Integer.parseInt(previouscalories);

        Intent intent = getIntent();
        String newcalories = "";
        if (intent.hasExtra("passedvalue"))
        {
            newcalories = intent.getExtras().getString("passedvalue");

        } else {
            newcalories = "0";  
        }

        int numnew = Integer.parseInt(newcalories);
        int overall = numnew + numold;
        calories.setText("Current Calories:"+ overall);

        Button add = (Button)findViewById(R.id.add_meal);
        Button reset = (Button)findViewById(R.id.reset);
        Button about = (Button)findViewById(R.id.about);

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
                        startActivity(intent);
            }
        });


        about.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),about.class);
                startActivity(intent);
            }
        });
    }





    }

Main Activity 2

public class MainActivity2 extends ActionBarActivity {


    int sub_weight = 0;

    EditText weight;
    TextView calories;
    Button display, save;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        weight = (EditText)findViewById(R.id.edit_weight);
        calories = (TextView)findViewById(R.id.cal_total);
        display = (Button)findViewById(R.id.display);

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView calories = (TextView)findViewById(R.id.cal_total);
                String g = calories.getText().toString();
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("passedvalue", g );
                startActivity(intent);
            }
        });

    }
    public void onRadioButtonClicked(View v){

        boolean checked = ((RadioButton) v).isChecked();

        switch (v.getId()) {
            case R.id.radiopork:
                if (checked)
                    sub_weight = sub_weight + 2;
                break;
            case R.id.radiochicken:
                if (checked)
                    sub_weight = sub_weight + 7;
                break;
            case R.id.radiobeef:
                if (checked)
                    sub_weight = sub_weight + 9;
                break;
            case R.id.radiosalmon:
                if (checked)
                    sub_weight = sub_weight + 13;
                break;
            case R.id.radiocod:
                if (checked)
                    sub_weight = sub_weight + 17;
                break;
            case R.id.radiocereal:
                if (checked)
                    sub_weight = sub_weight + 18;
                break;
            case R.id.radioporridge:
                if (checked)
                    sub_weight = sub_weight + 23;
                break;
            case R.id.radiotoast:
                if (checked)
                    sub_weight = sub_weight + 26;
                break;
            case R.id.radiocrisps:
                if (checked)
                    sub_weight = sub_weight + 29;
                break;
            case R.id.radionoodle:
                if (checked)
                    sub_weight = sub_weight + 33;
                break;


        }

        }
    public void display_calories(View v){

        String m = weight.getText().toString();
        int x =  Integer.parseInt(m);
        int y = x * sub_weight;
        calories.setText(y+"");
        sub_weight = 0;

    }


}
Mike12345
  • 15
  • 3

2 Answers2

0

TextView calories = (TextView)findViewById(R.id.overall);

Each time the activity is created it does not know its previous state unless you code it that way, so calories will not have any value except if you have added something in the xml yourself as a default value (which will not be what the user had entered before).

If you want to save what the user enters between creation/deletion of the activity either send the result from the second activity back to the first (see here) or save the value on a sharedPreference (or database but that might be overkill for a single value).

Evripidis Drakos
  • 872
  • 1
  • 7
  • 15
0

Save the previous value in a static variable so that you can kept the value reusable.

public class MainActivity extends ActionBarActivity {
private static String previouscalories;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView calories = (TextView)findViewById(R.id.overall);
        int numold = Integer.parseInt(previouscalories);

        Intent intent = getIntent();
        String newcalories = "";
        if (intent.hasExtra("passedvalue"))
        {
            newcalories = intent.getExtras().getString("passedvalue");

        } else {
            newcalories = "0";  
        }

        int numnew = Integer.parseInt(newcalories);
        int overall = numnew + numold;
        previouscalories = String.valueOf(overall);
        calories.setText("Current Calories:"+ overall);
Rakhi
  • 81
  • 12