0

Use Andriod studio, Java Display the screen with corresponding food items. For example, the vegetable screen will display kale, onions,.... Use check boxes to show the food items. Then selected checkbox food item and prices will be displayed on checkout screen(another activity)

(send values(total price, name) of checked checkboxes in one activity to another activity)

Below is what I have now

public class Vegetables extends FoodTypesMenu  {

    CheckBox cb1, cb2, cb3, cb4;

    String name = "";
    String vegprice = "";

    double vegPrice = 0;

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



    }

    public void onCheckboxClicked(View view) {
        //Is the button now checked?
        boolean checked = ((CheckBox) view).isChecked();

        Button btnNext = (Button) findViewById(R.id.bn1);

        switch (view.getId()) {
            case R.id.onion_cb:
                if(checked){
                   name="Onion";
                   vegPrice += 5;

                }
                break;
            case R.id.carrot_cb:
                if(checked){
                  name="Carrot";

                }

                break;
            case R.id.spinach_cb:
                if(checked){
                    name="Spinach";


                }
                break;
            case R.id.garlic_cb:
                if(checked){
                    name="Garlic";


                }
                break;

        }

    }

    public void tocheckout(View v) {

        Intent intent = new Intent(this, CheckOut.class);
        intent.putExtra("Name", name);
        startActivity(intent);
        startActivity(intent);

    }

    }
```

```
public class CheckOut extends FoodTypesMenu {



    String name = "";
    String price = "";


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



        String name = getIntent().getStringExtra("Name");

        }

        public void OnClickButton (View view){
            Intent intent2 = new Intent(this, Payment.class);
            this.startActivity(intent2);
    }




    }



The string name is not displayed and also don't know how to display total price of checked boxes on another activity

[enter image description here][2]


  [1]: https://i.stack.imgur.com/Mag7P.png
  [2]: https://i.stack.imgur.com/1kw6X.png
Zoe
  • 27,060
  • 21
  • 118
  • 148
JaneJin
  • 5
  • 1
  • 3
  • You can use a `Hashmap` to store the data and pass it as an `Serializable` to the Intent which fire ups the next activity. – Shubham Panchal Jun 02 '19 at 02:03
  • check it out, maybe it's what you need! https://stackoverflow.com/questions/14333449/passing-data-through-intent-using-serializable – TaQuangTu Jun 02 '19 at 03:25

1 Answers1

0

Try this

 // modify your tocheckout() method
  public void tocheckout(String name,double vegPrice) {
    Intent intent = new Intent(this, CheckOut.class);
    intent.putExtra("name",name);
    intent.putExtra("vegPrice",vegPrice);
    startActivity(intent);

}
// call the above function from your onclick of checkbox
  switch (view.getId()) {
        case R.id.onion_cb:
            if(checked){
               name="Onion";
               vegPrice += 5;
               tocheckout(name,vegPrice);
            }
            break;
}


// and final get name and price in your CheckOut activity like this
  String name=getIntent().getStringExtra("name");
  double price=getIntent().getDoubleExtra("vegPrice",-1); //-1 is default val
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7