03-03-2009 03:13 AM
Hi.
I am creating a dialog box which allows users to key in some text inside. I am building on version 4.1. After keying in the text, user would choose either Ok or Cancel. Here is the code for it:
final class TestDialog extends Dialog{
private EditField userNameField;
private ButtonField okButton;
private ButtonField cancelButton;
public TestDialog(String choices[],int values[]){
super("Enter a username", choices,values,Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.QUESTION), Dialog.GLOBAL_STATUS);
userNameField = new EditField("User Name: ", "", 50, EditField.EDITABLE | EditField.FILTER_PHONE);
net.rim.device.api.ui.Manager delegate = getDelegate();
if( delegate instanceof DialogFieldManager){
DialogFieldManager dfm = (DialogFieldManager)delegate;
net.rim.device.api.ui.Manager manager =dfm.getCustomManager();
if( manager != null ){
manager.insert(userNameField, 0);
}
}
}
public String getUsernameFromField(){
return userNameField.getText();
}
}
I am however, facing problems in retrieving which button user has selected (OK or CANCEL). When selecting OK, nothing happens. When selecting CANCEL,I would get an ArrayIndexOutOfBounds error.
String choices[] = {"Ok","Cancel"};
int values[] = {Dialog.OK,Dialog.CANCEL};
TestDialog pw = new TestDialog(choices,values);
System.out.println(""+pw.doModal());
if (pw.doModal() == Dialog.OK) {
...
}
Does any of you have any idea about this? Thanks in advance.
Hanisah
03-06-2009 11:35 AM
You are passing Dialog.OK into the constructor for Dialog in the parameter that specifies the default choice. This choice value is the index of the array (choices[] in your case) that you want selected by default. Dialog.OK is a constant for the Dialog class, and won't match up with your array position.
I recommend having a look at the VoiceNotesDemoDialog class that is part of the voicenotesdemo included with the BlackBerry JDE. It is an example of a custom Dialog that prompts for user input.
05-26-2009 03:06 PM
05-26-2009 05:02 PM
Try with the following:
class TestScreen extends MainScreen { private Dialog diag; public TestScreen() { String choices[] = {"Ok","Cancel"}; int values[] = {Dialog.OK,Dialog.CANCEL}; diag = new TestDialog(choices,values); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { int iResponse = diag.doModal(); if(iResponse == 0) { System.out.println("OK Pressed"); } else { System.out.println("Cancel Presses"); }
} }); } }
Regards
Bikas
05-26-2009 06:09 PM
Minor point, possibly more accurate to use:
if(iResponse == Dialog.OK)
11-13-2009 11:04 PM
I realize this thread is old, but I just came across it and did figure out how to avoid the array out of bounds exception.
Add an additional value to the front of the values[] array to be a 'placeholder' for for editfield.
int values[] = {0,Dialog.OK,Dialog.CANCEL};