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: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

how to get value of chosen choice in question dialog?

hi, i use such a code to show a question (yes/no) dialog in a background app:

 

public void askUserBackgroundApp(String message) {

ui = Ui.getUiEngine();
Dialog dialog = new Dialog(message,
new String[]{"Yes", "No"},
new int[]{Dialog.YES, Dialog.NO},
Dialog.D_YES_NO,
Bitmap.getPredefinedBitmap(Bitmap.QUESTION));

screen = dialog;
ui.queueStatus(screen, 1, true);


}

 

how can i get the chosen value? i mean, how can i get if the user chose Dialog.YES or Dialog.NO ?

 

regards

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: how to get value of chosen choice in question dialog?

 

int response = dialogScreen.getSelectedValue();

if(Dialog.YES == response)
{

      ---------

}

 


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to get value of chosen choice in question dialog?

i've tried this before. but dialog.getSelectedValue() does not return me what i expect. if i modify my funcion like that:

 

public int askUserBackgroundApp(String message) {

ui = Ui.getUiEngine();
Dialog dialog = new Dialog(message,
new String[]{"Yes", "No"},
new int[]{Dialog.YES, Dialog.NO},
Dialog.D_YES_NO,
Bitmap.getPredefinedBitmap(Bitmap.QUESTION));

screen = dialog;
ui.queueStatus(screen, 1, true);

return dialog.getSelectedValue();
}

 

and later i try to check what was chosen:

if(askUserBackgroundApp("my question") == Dialog.YES){

  System.out.println("yes");

} else{

  System.out.println("no");

}

 

i alywas get no.. i've also checked int values- for Dialog.YES i got 4 and for dialog.getSelectedValue() i got 3..

 

 

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

Re: how to get value of chosen choice in question dialog?

I don't think the code supplied by BBDeveloper will work in this instance because the

 

ui.queueStatus(screen, 1, true);

 

is not blocking.  In other words, if this

 

int response = dialogScreen.getSelectedValue();

 

was run immediately after the queue(...) it would get the value set in the dialog rather than the value specified by the user.

 

This must be made modal or you will need to use a DialogClosedListener to get the value.  From the looks of this, you are executing this from a background task, that doesn't have a UiApplication associated, so I think your only choice is a DialogClosedListener.

 

However you could try replacing the queue(..) with this:

UiApplication.getUiApplication.invokeAndWait(new Runnable() {

public void run() {

dialogScreen.doModal();

}

});

then the

int response = dialogScreen.getSelectedValue();

will work correctly.

 

Try this and let us know what Exception you get if it does not work.

 

Note code was just written, probably won't compile, but hopefully you can fix that....

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: how to get value of chosen choice in question dialog?

[ Edited ]

Your "return" statement will not wait for the dialog to be dismissed, you are placing the dialog job on the event queue (asynchronously).

 

Is there a reason why you are doing this? Is this called form a background thread?

 

If so, then you should use DialogCloseListener instead.

 

 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to get value of chosen choice in question dialog?

yes, this is a background app (i've written it in a first sentence of my first post). i can try using dialogClosedListener, but i use almost the same code for showing Dialog.OK :

 

public int informUserBackgroundApp(String message) {

ui = Ui.getUiEngine();

Dialog dialog = new Dialog(message,
new String[]{"Ok"},
new int[]{Dialog.OK},
Dialog.OK,
Bitmap.getPredefinedBitmap(Bitmap.INFORMATION));

screen = dialog;
ui.queueStatus(screen, 1, true);

return Dialog.OK;
}

 

and here, when i use it:

if(informUserBackgroundApp("mess") == Dialog.OK){

//TODO logic here

}

 

it works as i want. i mean-firstly Dialog with a message is shown and the logic is done after the user clicks on OK!

so i thought that maybe i can do Dialog.YES_NO also without a listener...

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: how to get value of chosen choice in question dialog?

Did you try invokeAndWait() ?

 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: how to get value of chosen choice in question dialog?

but i think that invokeAndWait is for UiApplication.. mine is background one..

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: how to get value of chosen choice in question dialog?

ahh...then you seem to be stuck with the listener.

 

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

Re: how to get value of chosen choice in question dialog?

You said:

"it works as i want. i mean-firstly Dialog with a message is shown and the logic is done after the user clicks on OK!

so i thought that maybe i can do Dialog.YES_NO also without a listener..."

 

Doesn't look like it will to me.  Put breakpoints in to prove that that processing does wait for the Dialog box to be clicked.  I think you will be surprised.