0

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?

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Krish
  • 17
  • 2

4 Answers4

3

Use Field.setCookie(Object) and Field.getCookie() to set and retrieve a cookie on the field. You can use this to match up the edit field the way you want.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
1
EditField[] ef = new EditField[size];//at global in class

//Do something

VerticalFieldManager vfm = new VerticalFieldManager();
for(int i=0 ;i<size ;i++)
{
    HorizontalFieldManager hfm = new HorizontalFieldManager();
    ef[i] = new EditField();
    hfm.add(ef[i]);
    vfm.add(hfm);
}
add(vfm);

//complete Creating UI

// Start getting value from EditField

public void fieldChanged(Field field, int context) {
    super.fieldChanged(field, context);
    for(int i=0;i<size;i++)
    {
        if (field.equals(ef[i]) {
            system.out.println("Value from editfield :- " + ef[i].getText());
        }
    }
}
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • I write this code in notepad so if there is any spelling mistake in code or error regarding spelling then please solve it. – V.J. Dec 03 '11 at 15:51
  • Is there any way to set and get tag or id or name to the field so that value from specific field can be fetched? – Krish Dec 03 '11 at 16:04
  • @Ajmal Please be specific to the question. – Krish Dec 03 '11 at 16:41
  • @Krish i do not understand what you want to implement. Please give brief description of your requirements. What is tag,id or name ...??? – V.J. Dec 04 '11 at 05:44
1

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.

Xiangdong
  • 254
  • 2
  • 10
1

You can Try like this

Editfield ef = new Editfield();
add(ef);
ef.setcookie("Here you can set object with specific value");
ef.setchangelistner(this);

and in field change listner function

public void fieldChanged(Field field, int context) {
    object ob = field.getcookie();
    // compare this ob and give appropriate actionss
}
Stack User 5674
  • 1,548
  • 1
  • 20
  • 44