07-17-2012 08:11 AM
I am adding ObjectChoiceFields at runtime in a for loop. Suppose value of i in for loop is 3,I wl get 4 ObjectChoiceFields. how to get value of first ObjectChoiceField?
07-17-2012 08:59 AM
I have an app where I also add ObjectChoiceFields at runtime with a loop. In my case, I save them in a Vector to reference them later. But you could also ask the their manager for them.
If you place all your choice fields within the same Manager, try using Manager.getField(int i) to retrieve them.
I hope that helps.
Cheers.
07-18-2012 03:22 AM
07-18-2012 04:44 AM
Can you explain your problem another way? Is the problem with detecting the selected item on the ObjectChoiceField or is it persisting the selected item?
If you have a problem persisting values, then can I recommend that you search the forum for information, there are a couple of KB articles and/or labs that discuss this.
07-18-2012 05:24 AM
I am adding ObjectchoiceFields at runtime. I want addition of selected values in ObjectchoiceFields (suppose 3 ObjectchoiceFields are added at runtime) should not be greater than 10.
Here is my code:
combo.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
if(field == combo)
{
Object ob = combo.getChoice(combo.getSelectedIndex());
String selectedValue = ob.toString();
int select = Integer.parseInt(selectedValue);
sum = sum+select;
if(sum > 10)
{
sum = sum-select;
combo.setSelectedIndex(0);
Dialog.alert("........");
}
}
}
07-18-2012 07:46 AM
And your problem is......
07-18-2012 09:03 AM
07-18-2012 10:06 AM
"In such case it goes inide if(sum>10) loop,then sum becomes 7 again and it shows dialog.
Later i select 1st combo and i select value 5 then in such case sum should be 9,bt in my code sum becomes 12."
But 7 plus 5 is 12, not 9. Why should it be 9?
07-18-2012 10:28 AM
07-18-2012 10:49 AM
As peter suggested, you should check some KB articles and labs about persistance values.
You could compute the sum every time instead of accumulate it with the method I suggested before, so you have the last value of every choice. Also, you could save in another object the values as they vary to modify only the latest change.
Anyway, I believe the question is getting out of scope here and we are solving more a logical problem than an ObjectChoiceField problem.
Either you compute the sum starting from zero every time a choice is changed or you save somewhere (in memory, a specific object, int[], Vector, persistance, database, whatever) the values of all choices so you can do something like sum-oldValue+newValue.
Think about how would you solve it if instead of ObjectChoiceField you were using just regular integers. If you changed them from time to time, how will you update the total sum of them? That will lead you to solve the problem, I think.