12-17-2010 07:38 AM
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);
}
});
}
}
Solved! Go to Solution.
12-17-2010 08:02 AM
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);
}
12-18-2010 01:34 AM
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......
12-18-2010 01:50 AM
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.
12-18-2010 05:10 AM - edited 12-18-2010 05:10 AM
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.
12-18-2010 07:04 AM
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);
}
}
12-18-2010 10:45 AM
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??
12-18-2010 12:31 PM
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.
05-27-2011 07:46 AM
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 ..?