Checkbox calculations in Android
Checkbox calculations in Android
I want to do a restaurant apk but I am stuck.
How to assign a price to a checkbox item and get the sum of all checked items and display them in a textView?
□item1 $5
□item2 $7
□item3 $10
□item4 $8
textViewer "Total: "
//checked items
Start at the beginning by breaking this into smaller pieces. First make a view with a list of items. Then add the checkboxes. Continue from there.
– Code-Apprentice
Aug 19 at 21:22
1 Answer
1
//make checkbox as an array list
ArrayList<CheckBox> checkList = new ArrayList<CheckBox>();
CheckBox mCheckBox1, mCheckBox2;
TextView tvExample;
int count;
int price;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheckBox1 = (CheckBox)findViewById(R.id.checkBox2);
mCheckBox2 = (CheckBox)findViewById(R.id.checkBox3);
tvExample = (TextView)findViewById(R.id.tvExample);
//add checkboxes to array list
checkList.add(mCheckBox1);
checkList.add(mCheckBox2);
Button bInput = (Button)findViewById(R.id.bInput);
bInput.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
for(int i=0 ; i<checkList.size(); i++)
//check if the checklist is checked
if(checkList.get(0).isChecked() == true)
//when it's checked it's incremented
count++;
//add price
price+=5;
//add more if statement for other items
//set the value to your TextView here
tvExample.setText(Integer.toString(price));
//restart the count
count = 0;
price = 0;
);
Sorry my question wasn't well explained
– Romain Sirius
Aug 20 at 4:55
edited that, you can assign price with if statement and change the variable price, and then you can restart the price counting after set it on your textview
– Mochammad Hafidz
Aug 20 at 21:20
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Would be useful if you added a Minimal, Complete, and Verifiable example of your code
– cricket_007
Aug 19 at 21:20