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
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

ObjectChoiceField problem

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?

Developer
lagunex
Posts: 64
Registered: ‎02-12-2011
My Device: 9300

Re: ObjectChoiceField problem

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.

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: ObjectChoiceField problem

In the objectchoicefield i am using int values. when i select some values in objectchoicefield i want to persist selected values somewhere ,where i can change those values. How can i do this?
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField problem

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. 

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: ObjectChoiceField problem

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("........");
}

}

}

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField problem

And your problem is......

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: ObjectChoiceField problem

When i select 1st ObjectchoiceField (combo),suppose i select value 3 then value of sum becomes 3.
When i select second combo,suppose i select value 4 then value of sum becomes 7.
when i select 3rd combo,suppose i select value 7 then value of sum becomes 14.
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.
I want that when i change value of 1st and second combo,the value should be added in sum accordingly.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: ObjectChoiceField problem

"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?

Developer
JoVinz
Posts: 255
Registered: ‎05-03-2012
My Device: 9930

Re: ObjectChoiceField problem

Thats what my point is...selected value of 1st combo should be replaced by new selected value.earlier In first combo i am selecting value as 3 and in second combo its 4. Later in 1st combo i select value as 5. so sum should be 5+4=9.
Developer
lagunex
Posts: 64
Registered: ‎02-12-2011
My Device: 9300

Re: ObjectChoiceField problem

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.