user-interfaceblackberrydynamic-uiblackberry-editfield

How to get value from dynamically generaged editfield in Blackberry


I need to generate ui component dynamically at runtime. I am able to generate the component, but my problem is how to get value entered in dynamically generated field?

I am doing something like this.

    final HorizontalFieldManager hfm = new HorizontalFieldManager();

    for (int i=0; i<5; i++)
    {
        hfm.add(new EditField());
    }

Is there any way to set tag of field and later we could find control by tag?


Solution

  • As far as I know, there is no way to set a unique id for an EditField. Maybe you can use a sub class of it, and implement your own unique id mechanism. Or, as your HorizontalFieldManager holds nothing but EditField, you can get a field by position, and cast it to EditField. Like this:

    Field f = hfm.getField(index);
    EditField ef = (EditField)f;
    

    UPDATE:

    public class MyEditField extends EditField {

    private int _id;
    
    public MyEditField(int id) {
        _id = id;
    }
    
    public int getID() {
        return _id;
    }
    

    }

    class MyHfm extends HorizontalFieldManager {

        //This is a cache, which holds all EditFields.
    



    private IntHashtable _editfields = new IntHashtable();

        public EditField getById(int id) {
            EditField ef = (EditField)_editfields.get(id);
            return ef;
    }
    
        public void add(Field f) {
            super.add(f);
            if (f instanceof MyEditField) {
                _editfields.put(((MyEditField)f).getID(), f);
            }
        }
    
        public void delete(Field f) {
            super.delete(f);
            if (f instanceof MyEditField) {
                _editfields.remove(((MyEditField)f).getID());
            }
        }
    

    }

    And then you can do like this:

        MyHfm hfm = new MyHfm();
        hfm.add(new MyEditField(0));
        hfm.add(new MyEditField(1));
        //get the field with id
        EditField f = hfm.getById(1);
    

    There are some other delete/add methods, remember to handle the cache inside them.