Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
kiran_b
Posts: 260
Registered: ‎10-27-2010
My Device: 8520
Accepted Solution

How to get multiple checkbox field data

 

Hi i am creating a dynamic checkbox fields by retrieving data from a vector.

if i select one checkbox field i am getting the string text of that field easily...

 

But the problem with when i select multiple checkboxes i am getting only last checkbox data....

 

can any one tell me how can i get all selected checkbox fields data in single time.......

 

Below is my code snippet:

 

class Sample extends MainScreen {
    Field field;
    int context;
    CheckboxField cb;
     CheckboxField cbField;
    int mCheckBoxesCount = 5;
    Vector v=StoreContact.retrieve();
   public static  StringBuffer sbi;
    Vector mCheckBoxes = new Vector();
   
    public Sample() {
       
        for (int i = 0; i <v.size(); i++) {
            CheckboxField cb = new CheckboxField((String)v.elementAt(i), false);
               
          this.add(cb);
          mCheckBoxes.addElement(cb);
       FieldChangeListener fli=new FieldChangeListener()
        {
           public void fieldChanged(Field field,int context)
            {
             boolean mProgrammatic = false;
                 
                if (!mProgrammatic) {
                mProgrammatic = true;
                cbField = (CheckboxField) field;
                int index = mCheckBoxes.indexOf(cbField);
          
                if (cbField.getChecked())
                {
                   for(int i=0;i<v.size();i++)
                   {
                   // Dialog.inform("Selected::" + cbField.getLabel());
                   sbi=new StringBuffer();
                   sbi.append(cbField.getLabel());
                   
                   }                 
                }
              
                mProgrammatic = false;
            }
          
            }
        };
        cb.setChangeListener(fli);
        }
        
       
        
    }    
 
    protected void makeMenu(Menu menu, int instance)
    {
        super.makeMenu(menu, instance);
          menu.add(new MenuItem("Get", 2, 2)
         {
            public void run()
             {
                
             Dialog.inform("Selected checkbox text::" + sbi);
             
             
             }
        });
       
    }
}

Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800
My Carrier: airtel

Re: How to get multiple checkbox field data

this listener is attached to all 5 checkboxes. when you are checking more than one checkboxs. every time fieldChanged called.Now see for last checkbox.

 

FieldChangeListener fli=new FieldChangeListener()
        {
           public void fieldChanged(Field field,int context)
            {
             boolean mProgrammatic = false;
                 
                if (!mProgrammatic) {
                mProgrammatic = true;
                cbField = (CheckboxField) field;
                int index = mCheckBoxes.indexOf(cbField);
          //cbField is last checked checkbox.
                if (cbField.getChecked())
                {
                   for(int i=0;i<v.size();i++)
                   {
                   // Dialog.inform("Selected::" + cbField.getLabel());
                   sbi=new StringBuffer();
                   sbi.append(cbField.getLabel());
                   //here in loop you are creating a new stringbuffer everytime and appending the value of last checkbox.
                   }                 
                }
              
                mProgrammatic = false;
            }
          
            }
        };
        cb.setChangeListener(fli);
        }

Please use plain text.
Developer
kiran_b
Posts: 260
Registered: ‎10-27-2010
My Device: 8520

Re: How to get multiple checkbox field data

 

Thanks for response...

 

I understand what you are saying, actually i need all checkbox values instead of last checkbox...

 

will you please tell me how can i get all selected checkbox values......

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: How to get multiple checkbox field data

As the vivart says that when you checked the checkbox, every time fieldChanged Called.

Then Put the Condition in the fieldChanged with a counter.

When the first checkbox will checked then counter value will increment by 1. and for the next checkBox checked then the condition will check that which checkbox is checked. and every time when the fieldchanged calledup You have to store values in a diffrent vector.

------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

Please use plain text.
Developer
gmuhammad
Posts: 55
Registered: ‎07-30-2009
My Device: Not Specified

Re: How to get multiple checkbox field data

[ Edited ]

class Sample extends MainScreen {
    Field field;
    int context;
    CheckboxField cb;
     CheckboxField cbField;
    int mCheckBoxesCount = 5;
    Vector v=StoreContact.retrieve();
   public static  StringBuffer sbi=new StringBuffer();
    Vector mCheckBoxes = new Vector();
   
    public Sample() {
       
        for (int i = 0; i <v.size(); i++) {
            CheckboxField cb = new CheckboxField((String)v.elementAt(i), false);
               
          this.add(cb);
          mCheckBoxes.addElement(cb);
       FieldChangeListener fli=new FieldChangeListener()
        {
           public void fieldChanged(Field field,int context)
            {
             boolean mProgrammatic = false;
                 
                if (!mProgrammatic) {
                mProgrammatic = true;
                cbField = (CheckboxField) field;
                int index = mCheckBoxes.indexOf(cbField);
          
                if (cbField.getChecked())
                {
                   //for(int i=0;i<v.size();i++)
                   //{
                   // Dialog.inform("Selected::" + cbField.getLabel());
                   sbi=new StringBuffer();
                   sbi.append(cbField.getLabel());
                   
                   //}                 

                }               

             
                mProgrammatic = false;
            }
          
            }
        };
        cb.setChangeListener(fli);
        }
        
       
        
    }   

 

hope this will work for you.

Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800
My Carrier: airtel

Re: How to get multiple checkbox field data

i think you dont need fieldchanged listener.

 

try this

 

class Sample extends MainScreen {
	int mCheckBoxesCount = 5;

	Vector v = StoreContact.retrieve();
	public static StringBuffer sbi = new StringBuffer();
	Vector mCheckBoxes = new Vector();
	VerticalFieldManager checkBoxGroup = new VerticalFieldManager();

	public Sample() {

		CheckboxField[] cb = new CheckboxField[v.size()];
		for (int i = 0; i < v.size(); i++) {
			cb[i] = new CheckboxField((String) v.elementAt(i), false);

		}
		add(checkBoxGroup);
	}

	protected void makeMenu(Menu menu, int instance) {
		menu.add(new MenuItem("Get", 2, 2) {
			public void run() {

				for (int i = 0; i < checkBoxGroup.getFieldCount(); i++) {
					CheckboxField checkboxField = (CheckboxField) checkBoxGroup
							.getField(i);
					if (checkboxField.getChecked()) {
						sbi.append(checkboxField.getLabel()).append("\n");
					}
				}
				Dialog.inform("Selected checkbox text::" + sbi);

			}
		});
		super.makeMenu(menu, instance);
	}
}

 

 

Please use plain text.
Developer
kiran_b
Posts: 260
Registered: ‎10-27-2010
My Device: 8520

Re: How to get multiple checkbox field data

Hi thanks for your reply...

 

       But the thing is your code is not working ...

i think if we remove field change listeners it may not effect as per changes??

Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800
My Carrier: airtel

Re: How to get multiple checkbox field data

 But the thing is your code is not working ...

 

i have tested this code its working for me.

 

i think if we remove field change listeners it may not effect as per changes??

 

i have seen your first post, you want checked checkbox label in menu item.

 So i thought that you dont need fieldChanged listener.

Please use plain text.
Contributor
nileshpawate
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: How to get multiple checkbox field data

Hie,

this code is very good...

but can u help me out for 1 more question...

 

this is working with selected choice What if i deselect it............if mean if that present in buffer then it should removed ..?

Please use plain text.