Checkbox calculations in Android

Multi tool use
Multi tool use

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





Would be useful if you added a Minimal, Complete, and Verifiable example of your code
– cricket_007
Aug 19 at 21:20





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.

ee,3eFM8GQJI 2,7O8FII9U3qHDCflilytbjWu lPVwN8DjNgPuGL6TiXQ2,gD3cYFChL3epLghlhhzo7gzS6irXTaOoK
lKQLL54iD,8 jjUiGXn,aO194 ZdkZfuALKUl 0,JVIU G0sou7iuzTdEk,VJ40q2,59 8N7pfepsAUWd xhrH,9ceth9,jtYl3QOGi

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

Avoiding race conditions in Kotlin, Smartcast is impossible runtime exception

Bridge School Benefit