Assigning specific value to checkbox

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Assigning specific value to checkbox



I'm currently coding a questionnaire whereby when each question is answered, a numerical value is assigned. I can do that successfully with the radiobuttons using the following code:


radioGroup1f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int a)
int finalScore = Values.getInstance().updateScore(a);
tvf.setText(" " + finalScore);


);
radioGroup2f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int b)
int finalScore = Values.getInstance().updateScore(b);
tvf.setText(" " + finalScore);

);



How do I apply a similar coding to checkboxes so that if it is checked, a value is assigned and if it isn't the value should be 0? I have tried something like this but it doesn't work


radioGroup9f = (RadioGroup) view.findViewById(R.id.radioGroupQ9F);
tvf = (TextView) view.findViewById(R.id.textView4f);
checkBox8a = (CheckBox) view.findViewById(R.id.checkBoxQ8Fa);
checkBox8b = (CheckBox) view.findViewById(R.id.checkBoxQ8Fb);
checkBox8c = (CheckBox) view.findViewById(R.id.checkBoxQ8Fc);
checkBox8d = (CheckBox) view.findViewById(R.id.checkBoxQ8Fd);
checkBox8e = (CheckBox) view.findViewById(R.id.checkBoxQ8Fe);
checkBox8f = (CheckBox) view.findViewById(R.id.checkBoxQ8Ff);



radioGroup9f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int a)
int finalScore = Values.getInstance().updateScore(a);
tvf.setText(" " + finalScore);


);


checkBox8a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);

checkBox8b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean c)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);

checkBox8c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean d)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);

checkBox8d.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean e)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);

checkBox8e.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean f)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);
checkBox8f.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean g)
int finalScore = Values.getInstance().updateScore(7);
tvf.setText(" " + finalScore);


);


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

if (radioGroup9f.getCheckedRadioButtonId() == -1)
Toast.makeText(getActivity(), "All Questions Need to be Answered", Toast.LENGTH_SHORT).show();
else

((MainActivityFemale) getActivity()).setViewPager(3);


);


return view;


}



}



The values page is as follows:


public class Values

private static Values instance;
private int QUESTIONS_COUNT = 45;
private int scores = new int[QUESTIONS_COUNT];

private Values()



//Todo take a look with regards to thread safe access
public static Values getInstance()
if(instance == null)
instance = new Values();

return instance;


public int updateScore(int checkedId)
switch(checkedId)

case R.id.checkBoxQ8Fa:
scores[7] = (8);
break;
case R.id.checkBoxQ8Fb:
scores[7] = (0);
break;
case R.id.checkBoxQ8Fc:
scores[7] = 6;
break;
case R.id.checkBoxQ8Fd:
scores[7] = 6;
break;
case R.id.checkBoxQ8Fe:
scores[7] = 10;
break;
case R.id.checkBoxQ8Ff:
scores[7] = 10;
break;

//Q9 for female
case R.id.radioButtonQ9Fa:
scores[8] = (-5);
break;
case R.id.radioButtonQ9Fb:
scores[8] = (-5);
break;
case R.id.radioButtonQ9Fc:
scores[8] = 10;
break;
case R.id.radioButtonQ9Fd:
scores[8] = 10;
break;
















int finalScore = 0;
for(int score : scores)
finalScore += score;

return finalScore;





Any help is greatly appreciated




1 Answer
1



Even CheckBox has setOnCheckedChangeListener. You can do it as follows:


CheckBox


setOnCheckedChangeListener


checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b)
int finalScore = Values.getInstance().updateScore(compoundButton.getId());
tvf.setText(" " + finalScore);


);





it didn't work. The values for the checkbox just aren't adding up
– Daniel
Aug 6 at 3:27





@Daniel what does your updateScore() do?
– Sagar
Aug 6 at 3:28



updateScore()





I just updated my question to include the values activity. the Update score will increase the score till the final activity so that the user can view their overall score @Sagar
– Daniel
Aug 6 at 3:45





@Daniel can you update your question with entire method?
– Sagar
Aug 6 at 3:46





okay sure @Sagar
– Daniel
Aug 6 at 3:47






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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard