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
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified
Accepted Solution

how to get the value in ObjectChoiceField

hi,

I have added two objectchoiceFields in a HFmanager and when i try to do these , give me class cast exceptions

 

ObjectChoiceField field6=(ObjectChoiceField)row6.getOriginal();
         int index=field6.getSelectedIndex();
         String str=choiceItems[index];
        

 Also i tried this

ObjectChoiceField field6=(ObjectChoiceField)row6.getField(0);

 

Any suggestions!

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to get the value in ObjectChoiceField

Object getChoice(int index) is the correct method to use.

in your case:
field6.getChoice(field6.getSelectedIndex());
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 132
Registered: ‎05-07-2009
My Device: Curve
My Carrier: Vodafone

Re: how to get the value in ObjectChoiceField

I am assuming that "row6" is the instance of your HorizontalFieldManager.

 

If yes - 

 

getOriginal() is not going to work at all, it is a method of Field class, so that is out of question.

 

The second option is right , which says "ObjectChoiceField field6=(ObjectChoiceField)row6.getField(0);" and here it must be giving classclastexception is because, the field at 0th location is not the ObjectChoiceField. Try the getFieldCount() method which will tell you that if there are fields in the manager, try with a different index say "1".

 

Sameer Joshi.

Sameer Joshi
Blackberry developer.
Developer
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified

Re: how to get the value in ObjectChoiceField

Thanks simon,

i did this

 Object getChoice(int index)

but it gives me nullpointerexception

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to get the value in ObjectChoiceField

getSelectedIndex():
For an empty ChoiceField, returns -1.

you have to check for this, of course.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified

Re: how to get the value in ObjectChoiceField

Thanks simon,

 

give me a favour.

 

can you tell me, how can i apply getselectedindex()

 

bcoz i need to get the field first from my HFManager.

how can i do that bcoz i am getting class exception for that.

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Developer
Posts: 132
Registered: ‎05-07-2009
My Device: Curve
My Carrier: Vodafone

Re: how to get the value in ObjectChoiceField

Can you please try my solution?
Sameer Joshi
Blackberry developer.
Developer
Posts: 120
Registered: ‎06-23-2009
My Device: Not Specified

Re: how to get the value in ObjectChoiceField

Brother, class castexception is thrown when you try to convert in opposite direction in class hierarchy.

thats y

 ObjectChoiceField field6=(ObjectChoiceField)row6.getField(1);
         Object sindex=field6.getChoice(1); 

this gives me this cast exception.

 

My problem is , how can you get the ObjectChoiceField out from the HFManager if not then how can i get the selected choice from it.

Any suggestions!

Thanks & Regards

Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Highlighted
Developer
Posts: 132
Registered: ‎05-07-2009
My Device: Curve
My Carrier: Vodafone

Re: how to get the value in ObjectChoiceField

Brother, i know why classcasexception is thrown -

 

First of all we need to see if the getField(index)  method is returning you valid ObjectChoiceFIeld or not, and if it is not, type casting it to (ObjectChoiceField) will definitely give you classcastexception.

 

Object obj = row6.getField(1 or 0 or watever integer);

if(obj instanceof ObjectChoiceField)

{

 field6 = (ObjectChoiceField)obj;

 Object sindex=field6.getChoice(1);

}

 

I am assuming - row6 = HorizontalFieldManager and field6 = ObjectChoiceField,

 

 

Sameer Joshi
Blackberry developer.
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to get the value in ObjectChoiceField

using the indizies of fields without check is inventing disaster.
i would save the fields in class variables, put them into a data structure or at least check for the code to return the correct object.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter