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
Contributor
Posts: 12
Registered: ‎10-10-2011
My Device: Torch 9800
My Carrier: Vodafone
Accepted Solution

Using a dropdown selection

Hi Everyone,

 

I've been scouring the web but can't find an answer that can quite help (I'm probably just looking in the wrong places :Detective: )

 

I have a drop down list, with several options, I want it so that when the user picks one of them I am able to move them onto the correct screen. The problem is I can get teh index they pick, and convert it to an object, but I can't figure out how to check that that object matches a given string. My code is below:

 

String BWChoices [] =
        {"Sit-up", "Crunch", "Press-up", "Pull-up", "Dip", "Plank", "Leg Raise"};
        //Create object for body weight weight choices
        ObjectChoiceField BWList = new ObjectChoiceField("Body Weight:", BWChoices, iSetTo);
        BWList.setFont(MyFont);
        _FMMain.add(BWList);
        int index = BWList.getSelectedIndex();
        Object activity = BWList.getChoice(index);
        activity.toString();
        if (activity.equals("Crunch"));
        {       
            LabelField proof = new LabelField("Proof");
            _FMMain.add(proof);
        }

 

The proof label is just there in an attempt to prove when it works, but it always shows :/

 

I have debugged it...when i get there activity does equal something else but it still shows the label.

Just tried to get it to actually go to a new screen using:

UiApplication.getUiApplication().pushScreen(new  YourScreen () );

still no luck...

 

Any ideas? Also is this the best way of doing it?

 

Really appreciate and help and thanks in advance.

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Using a dropdown selection

Just do this:

String BWChoices [] =
{"Sit-up", "Crunch", "Press-up", "Pull-up", "Dip", "Plank", "Leg Raise"};
//Create object for body weight weight choices
ObjectChoiceField BWList = new ObjectChoiceField("Body Weight:", BWChoices, iSetTo);
BWList.setFont(MyFont);
_FMMain.add(BWList);
int index = BWList.getSelectedIndex();
String labelString = "";
switch (index) {
   case 0:
	  labelString = "One";
	  break;
   case 1:
	   labelString = "Proof";
	   break;
   case 2:
	  labelString = "Two";
	  break;
   case 3:
	  labelString = "Three";
	  break;
   case 4:
	  labelString = "Four";
	  break;
   case 5:
	  labelString = "Five";
	  break;
   case 6:
	  labelString = "Six";
	  break;
	default:
	  break;
}

if (labelString.length() > 0) {
	LabelField lbl = new LabelField(labelString);
	_FMMain.add(lbl);
}

 

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

Re: Using a dropdown selection

[ Edited ]

Your problem is that you have not actually allowed the user to select.  There is no opportunity (time) between:

       _FMMain.add(BWList);

and

        int index = BWList.getSelectedIndex();
:for the user to actually enter/select anything.

 

You need to set the user actually select something, and then confirm it.  So you could add a "GO" Button to this screen, then tell the user to select the option they want and hit Go.  Now you need to set up a Field Changed listener, which waits until the "GO" Button is pressed, and in that processing, you check the selected Index.

 

Does this make sense?

 

 Edit - welcome to the forums!

Contributor
Posts: 12
Registered: ‎10-10-2011
My Device: Torch 9800
My Carrier: Vodafone

Re: Using a dropdown selection

Thanks for your replies, my only problem is that I ideally only want them to be able to select one option out of the four drop down lists I have. So with a "GO" button I would need to check that all the other dropdowns are set as "none" otherwise there would probably be issues.

 

With the FieldChangeListener is there something along the lines of "On Field Change"?

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

Re: Using a dropdown selection

FieldChangeListener on drop down list will get called for every change which makes it difficult to use in my opinion.   What if your user 'slips' and selects the wrong one, or looks at the list and makes no selection - the listener will still be called. 

 

Anything wrong with a go button for each of the drop down lists?

Contributor
Posts: 12
Registered: ‎10-10-2011
My Device: Torch 9800
My Carrier: Vodafone

Re: Using a dropdown selection

Ah I see your point especially with the touch screens at the moment :smileytongue: 

 

I've got it almost ( I think) my only problem is that ANY time I click go it goes to that screen, Here's my updated code:

 

int index = BWList.getSelectedIndex();
        final Object activity = BWList.getChoice(index);
        activity.toString();
       
    FieldChangeListener goListener = new FieldChangeListener(){
    public void fieldChanged(Field field, int context) {
        if (activity.equals("Crunch"));{
    UiApplication.getUiApplication().pushScreen(new  CrunchScreen () );
        }
        }
    };   
        ButtonField Go = new ButtonField("Go",10);
        Go.setChangeListener(goListener);
        _FMBottom.add(Go);

 

Had a thought whilst typing this...suggestable that I just use the index, makes it more difficult to remember which is which but once I've coded it once I won't need to again, is this easier than trying to get it to recognise a string?

 

Thanks for your help!

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

Re: Using a dropdown selection

Again, you are testing too early.

 

You can't check the index until the go button has been clicked, so this code:

 

        final Object activity = BWList.getChoice(index);
        activity.toString();


needs to be in the FieldChangeListener.

 

Regarding indexes verses String tests, when you conme back to update it in 3 months time, which is going to be easier to remember? 

 

That said, there are two things against using the String

1) It creates an uneeded Object - the String literal

2) If you change capitalisation, or use a different language, the String will need to be changed too. 

Contributor
Posts: 12
Registered: ‎10-10-2011
My Device: Torch 9800
My Carrier: Vodafone

Re: Using a dropdown selection

Right I've put it in the FieldChangeListener but still seem to get the page to change regardless of the value. I have checked and the activity.string is equal to the choice I set it to. But it Always moves on, never does it just do nothing (which seems off to want it to but it is for now)

 

FieldChangeListener goListener = new FieldChangeListener(){
    public void fieldChanged(Field field, int context) {
        //Unsure...
       
        try{
        if (BWList instanceof Field){
        int index = BWList.getSelectedIndex();
         final Object activity = BWList.getChoice(index);
        activity.toString();
        if (activity.equals("Crunch"));
    UiApplication.getUiApplication().pushScreen(new  CrunchScreen () );   
        }
       
                }   
        catch(ClassCastException e){
            e.printStackTrace();
            }
    }
    };   
      
      ButtonField Go = new ButtonField("Go",10);
        Go.setChangeListener(goListener);
        _FMBottom.add(Go);
       
    }

 

I have tried moving where the Go.setChangeListener is but to no avail, I have also tried it without the if (BWlist instanceof field) with no changes. So totally lost!

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

Re: Using a dropdown selection

Sorry I don't think we have enough here to really correct your code, so let us go with a little theory, since there does appear t be some confusion.

 

You have done this:

 

        Go.setChangeListener(goListener);

 

SO when the Go field changes, the listener will be called.  In your case, the Go Field is a ButtonField, so the listener is invoked when someone presses the Button.

 

public void fieldChanged(Field field, int context) {..}

 

will be called.  The context you can ignore for now, but you wil note that there is one defined in the JavaDoc, and you use this to distinguish user changes, from one that you make.  Not really applicable for a Button, but for other Fields, very useful.

 

When fieldChanged(Field, the Field passed in will be the Field that changed, so in your case will be the ButtonField Go.  You might want to check, in this case there is probably no point.

 

You have this:

if (BWList instanceof Field){

which seems redundant - what else is it going to be?  What is the purpose of this statement?

 

Similarly, since you know these to be set this way, you could probably simplify the following statements to

       int index = BWList.getSelectedIndex();
         final String activity = (String) BWList.getChoice(index);

and I'm not sure about the try/catch, but I guess it is not doing any harm.

You say

"But it Always moves on, never does it just do nothing (which seems off to want it to but it is for now)"

Sorry I do not understand this.  In what circumstances do you NOT want it to move on?  How is this reflected in the code?. 

 

Hope this helps.

Contributor
Posts: 12
Registered: ‎10-10-2011
My Device: Torch 9800
My Carrier: Vodafone

Re: Using a dropdown selection

Bingo! Got it, it was that it was checking for an object so it was always true, changing it to String allowed it to recognise exactly what it was I was looking for.So thank you.

 

When I didn't want it to move on was if the selected dropdown was set to anything but crunch. Have now added code so that if it's set to other values it goes to the relevant screen.

 

Thanks for your help (alot)