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
Trusted Contributor
trupti
Posts: 242
Registered: ‎03-16-2010
My Device: 9530
Accepted Solution

Retrieve value from Dialog

 

Hi,

 

   I have a class that extends Dialog. Few Radiobuttns are added. I want to get the value of selected RadioButton to the actual screen class calling the Dialog on clicking Dialog.OK button. The code I tried is as follows:

 

My Screen class :

                XXXDialog clScrn = new XXXDialog ();
                if (clScrn.doModal() == Dialog.OK) {
                    // I click Ok, but this is never shown
                    Dialog.alert("Closed & here");
                    Dialog.alert("Selected = " + clScrn.getSelectedItem());
                }
                clScrn = null;

 

The Dialog class -

  public XXXDialog() { 
        super(Dialog.D_OK, "Select Item", Dialog.OK, null, VERTICAL_SCROLL | VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT);
        rbg = new RadioButtonGroup();
        rb1 = new RadioButtonField("One", rbg, false, Field.USE_ALL_WIDTH);
        rb2 = new RadioButtonField("Two", rbg, false, Field.USE_ALL_WIDTH);

        rb1.setChangeListener(this);
        rb2.setChangeListener(this);

        add(rb1);
        add(rb2);
    }
    
    public void fieldChanged(Field f, int context) {
/*
Actually, I want to know which field was clicked RadioButton or Ok button. If it is Ok, then I want to hide and return back to screen.
*/

        try {
                RadioButtonField fd = (RadioButtonField) f;
                item = fd.getLabel();
                System.out.println("Selected = " + item);
        } catch (Exception e) {
        }
        
        try{
            ButtonField fd = (ButtonField) f;
            System.out.println("********* Button PRESSED = " + f);
            this.close();
        } catch (Exception e) {
        }
        
        System.out.println("*********** Field Count = " + f.getManager().getFieldCount() + " Index = " + f.getIndex());
    }

    public String getSelectedItem() {
        return item;
    }

 

The XXXDialog class is in a seperate java file.

Can anyone help me out what to do and how to achieve the goal.

 

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Gunjan
Posts: 50
Registered: ‎09-01-2009
My Device: Not Specified

Re: Retrieve value from Dialog

hey

i think you need to register dialog.setCloseListener and check for ok and cancel conditions.

Dialog confirmationDialog = new Dialog("ques",new String[]{"YES"},new int[]{Dialog.YES},Dialog.YES,null);

confirmationDialog.setDialogClosedListener(new DialogClosedListener(){
                        public void dialogClosed(Dialog aTheDialog,int aDialogReturnValue){}});

add your radio button now....

then with confirmationDialog.ask you can get your value

 

Trusted Contributor
trupti
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Retrieve value from Dialog

public XXXDialog() { 
        super(Dialog.D_OK, "Select Item", Dialog.OK, null, VERTICAL_SCROLL | VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT);

        setDialogClosedListener(new DialogClosedListener() {
               public void dialogClosed(Dialog dialog, int choice) {                            
                    dialog.close();
                }
            });

        rbg = new RadioButtonGroup();
        rb1 = new RadioButtonField("One", rbg, false, Field.USE_ALL_WIDTH);
        rb2 = new RadioButtonField("Two", rbg, false, Field.USE_ALL_WIDTH);

        rb1.setChangeListener(this);
        rb2.setChangeListener(this);

        add(rb1);
        add(rb2);
    }
    

 

I added the setDialogClosedLisener as you said, but no progress. Ok is never trapped.

 

If I remove the FieldListener fro mmy Dialog class then I can get the "Dialog Closed & here" dialog alert.  But as their is no fieldListener, so can't set which radiobutton is selected which is the data I want to retrieve.

 

>> then with confirmationDialog.ask you can get your value

I think I didn't get your point. I don't have to ask or get input fro mthe user, have the get teh selected Radiobutton's value on user clicking Ok. Dialog only has Ok button. I don' want to close the dialog on Esc button, how can that be done ?

 

 

Thanks

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Retrieve value from Dialog

the dialogclosedlistener is called when the dialog is closed.

you currently try to close it - again.

what you should do is process the value (int choice) you get as a parameter.

your process flow continues in the dialogClosed method.

----------------------------------------------------------
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
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Retrieve value from Dialog

Your original code should work fine with this change:

 

clScrn.doModal();
if ( clScrn.getSelectedValue() != Dialog.OK ) ..

Trusted Contributor
trupti
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Retrieve value from Dialog

 

I got my solution. What I did is :-

 

In my main screen, I call the dialog as

clScrn.doModal();

Dialog.alert(Come Back here");   // I can see this alert once dialog is closed.

 

In my dialog class, I have only Ok button, dialog can't be closed using Esc key as I implemetned the code -

   // Dialog should not close on Esc key
    public boolean keyChar(char key, int status, int time) {
        boolean retVal = false;
        switch(key) {
            case Characters.ESCAPE:
                break;
        }
        return retVal;
    }

 

So now there is no other means to close the dialog except, Ok button and I make sure that when Ok is clicked, 1 radiobutton is selected.

 

Thanks to all for the time and efforts.

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Trusted Contributor
trupti
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Retrieve value from Dialog

 

Yes Simon, I also found that dialogChangeListenr does no good to me. Infact the flow doesn't go in dialogClosed() event only.

 

Thanks


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.