0

I want to have a Form that can Add or Delete TextFields.

I was so far creating an array and resizing (actually copying original array to a new, larger array), then deleting all form elements, and adding everything again + this new array of TextFields

but I think this will slow down the program when there are many TextFields
Adding TextFileds to the Vector is not working. When it is about to add TextField to the Form,

form.append(vector.elementAt(i));

it says that the element is not it.

method Form.append(Item) is not applicable
  (actual argument Object cannot be converted to Item by method invocation conversion)
method Form.append(Image) is not applicable
  (actual argument Object cannot be converted to Image by method invocation conversion)
method Form.append(String) is not applicable
  (actual argument Object cannot be converted to String by method invocation conversion)

Should I countinue with resizing arrays, or is there a better way?

gnat
  • 6,213
  • 108
  • 53
  • 73
SmRndGuy
  • 1,719
  • 5
  • 30
  • 49

2 Answers2

2

According to Form documentation "The items contained within a Form may be edited using append, delete, insert, and set methods." And you also have a get method, so I don't think you need a Vector at all. Lets say you have:


    Form form = new Form("Multiple fields");

    // If you want to add a new TextField
    form.append(new TextField("label", "text", 10/*maxSize*/, TextField.ANY));

    // if you want to delete the last TextField:
    form.delete(form.size() - 1);

    // to iterate at all fields:
    for (int i = 0; i < form.size(); i++) {
        TextField textField = (TextField) form.get(i);
    }

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
0

To avoid compilation errors when adding to Form, explicitly cast Vector elements to needed type (Item):

form.append((Item)(vector.elementAt(i)));

Note if you are used to work with Java SE 5 or higher - keep in mind that Java ME is based on much older version (JDK 1.3). As a result you'd see much more explicit casts because generification is not an option.

gnat
  • 6,213
  • 108
  • 53
  • 73