08-24-2010 11:20 AM
I am trying to delete a Field from the screen when a delete event occurs in my app.
There are two ways to delete a particular item but they both use the same method/function.
When calling getManager() from directly in the Field it returns null.
When calling the Field.delete() method/function from a popup the getManager() method/function returns the Manager correctly.
Is there something something I am missing here?
08-24-2010 11:33 AM
Strange... getManager() will return null if your Field is not managed by anything at the moment. Are you sure your Field is displayed when you call it "directly in the Field"? Can the call happen at other times?
08-24-2010 11:40 AM - edited 08-24-2010 11:40 AM
It is definitely on the screen and most definitely should be attached to a Manager.
Basically the way it works is if a user presses the delete key while the Field is selected it will delete it (after a confirmation dialog of course) but getManager() returns null.
There is also a popup that gives you extra options on the field (something that looks a little similar to the Context Popups on OS6) that gives you the option to delete the Field and when you call the very same method for the Field in the popup it deletes fine.
No idea what the difference is and can't seem to find a solution.
08-24-2010 12:25 PM
On what level are you overriding keyChar (or wherever you catch that Delete key)? Care to share the relevant code?
08-24-2010 12:29 PM
protected boolean keyChar(char c, int status, int time) {
switch(c) {
case ('d' | 'D'):
case Keypad.KEY_DELETE:
case Keypad.KEY_BACKSPACE:
delete();
return true;
default:
return super.keyChar(c, status, time);
}
}
public String getListName() {
return _thisListName;
}
public void delete() {
int i = Dialog.ask(Dialog.D_DELETE, "Delete " + _thisListName + "?");
if(Dialog.DELETE == i) {
Manager m = getManager();
if(m != null)
m.delete(this);
}
}
Here is the relevant code. The keyChar to catch the delete key and the location of the delete method are in the one custom Field class.
08-24-2010 12:38 PM
Hmmm... Don't see any obvious problems with the code. I'll try to debug it at home tonight - there might be something small and unexpected (as it is often the case with BlackBerry).
08-24-2010 09:49 PM
Well, I've just created and tested the following field - with everything working as it should. Check what you are doing differently. I'm creating the first one with FOCUSABLE style bit, of course.
import net.rim.device.api.i18n.ResourceBundleFamily;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
public class DeletableLabelField extends LabelField {
private static int _fieldCount;
public DeletableLabelField() {
++_fieldCount;
}
public DeletableLabelField(Object arg0) {
super(arg0);
++_fieldCount;
}
public DeletableLabelField(Object text, long style) {
super(text, style);
++_fieldCount;
}
public DeletableLabelField(ResourceBundleFamily rb, int key) {
super(rb, key);
++_fieldCount;
}
public DeletableLabelField(Object text, int offset, int length, long style) {
super(text, offset, length, style);
++_fieldCount;
}
protected boolean keyChar(char c, int status, int time) {
switch(c) {
case ('d' | 'D'):
case Keypad.KEY_DELETE:
case Keypad.KEY_BACKSPACE:
delete();
return true;
case ('a' | 'A'): {
insert();
return true;
}
default:
return super.keyChar(c, status, time);
}
}
public void delete() {
int i = Dialog.ask(Dialog.D_DELETE, "Delete " + getText() + "?");
if(Dialog.DELETE == i) {
Manager m = getManager();
if(m != null)
m.delete(this);
}
}
public void insert() {
Manager m = getManager();
if (m != null) {
m.add(new DeletableLabelField(getText() + "_" + _fieldCount, FOCUSABLE));
}
}
}