Getting count of checkboxes selected, displaying results

Put your problem here if it does not fit any of the other categories.

Getting count of checkboxes selected, displaying results

Postby Paul72 » Sat Feb 18, 2012 4:14 am

Hello all, I am working on a project whereby I have a list of checkbox items and a calculate button. I want to have the code capture a count of the selected checkboxes and when the "calculate" button is selected, total up the number of checkboxes and return a message based on the percentage of checkboxes selected.

I'm having trouble on getting the correct logic for the onCheckChanged function, the running total amount, and the results message/activity. Any pointers you all can provide would be most welcome.

I'm mainly interested in getting the calculation of selected checkboxes and the "result" message to work at this point.

I've attached the code I have to date. You will see some notification activities in there as well. This code is perfectly functional and has nothing to do with the issue I'm having. I currently have the "calculate" button pointing to a web browser just as a placeholder.

Thank you very much in advance!!!
Attachments
CheckListApp.zip
(550.8 KiB) Downloaded 23 times
Paul72
Freshman
Freshman
 
Posts: 7
Joined: Sat Sep 25, 2010 8:08 pm

Top

Re: Getting count of checkboxes selected, displaying results

Postby Phyll » Sun Feb 19, 2012 12:09 am

Hi Paul72,

I'm kind of puzzled. How do you assemble a megabyte of code, sophistocated code that forces advertising messages onto the users devices and not be able to at least bruteforce the evaluation of a few checkboxes?

I guess if I were going to give you some suggestions, I would use something other than the xml to build those checkboxes so that they could be in an array and added to the view group in the code. Then you could count them in a loop.

You could also as I mentioned just brute force them in a block that inquires of each one and puts the resultant boolean value in an array. That seems pretty easy and in a megabyte of code not that wasteful I guess.

Hope that helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Getting count of checkboxes selected, displaying results

Postby Paul72 » Sun Feb 19, 2012 6:32 am

Thanks for the feedback, Phyll. In all honesty, I'm learning this code as I go. Finding snippets here and there, piecing stuff together. Keeping what works, trashing what doesn't. I don't have a computer science background, just doing this from trial and error.
Paul72
Freshman
Freshman
 
Posts: 7
Joined: Sat Sep 25, 2010 8:08 pm

Re: Getting count of checkboxes selected, displaying results

Postby Phyll » Sun Feb 19, 2012 1:20 pm

Hi Paul72,

Yes, the fact that this is some kind of trojan that loads xtify into the background without telling the user makes me question your intentions. None the less, you could try something like this if you are just totaling the number of checks. This is the brute force way I mentioned. It just makes one of the listeners for each checkbox and adjusts the score as they are clicked. The elegant way to do it would be to create an array of checkboxes and then add them to the layout in code. Look here:

http://abhishek347.wordpress.com/2012/0 ... heckboxes/

Code: Select all
public class MainActivity extends Activity {
   private PersistentLocationManager persistentLocationManager;
        int score = 0;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      final Context context = this;
            
      CheckBox ChkBx1 =
             ( CheckBox ) findViewById( R.id.checkbox1 );
         repeatChkBx.setOnClickListener(new OnClickListener()
         {
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
             {
                 if ( isChecked ) score++;
                    else score--;
             }

            @Override
            public void onClick(View arg0) {
               // TODO Auto-generated method stub
               
            }
         });

      CheckBox ChkBx2 =
             ( CheckBox ) findViewById( R.id.checkbox2 );
         repeatChkBx.setOnClickListener(new OnClickListener()
         {
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
             {
                 if ( isChecked ) score++;
                    else score--;
             }

            @Override
            public void onClick(View arg0) {
               // TODO Auto-generated method stub
               
            }
         });
Do the rest of them...



Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Getting count of checkboxes selected, displaying results

Postby Paul72 » Mon Feb 20, 2012 3:30 pm

thanks so much. FYI, the xtify piece is for receiving notifications via messaging and can be disabled by the user running the app at any time via the device menu.
Paul72
Freshman
Freshman
 
Posts: 7
Joined: Sat Sep 25, 2010 8:08 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests