1

For my Android application, I want to have a view which allows a user to click a plus button to add EditText fields, and next to the EditText fields, I want to have minus buttons that will remove them from the view. In essence, something that is very similar to adding multiple phone numbers/email addresses in the edit Contact interface on Android.

I imagine I will need to do this by inflating my main view with a separate one that contains the EditText and button I want to add each time. However, I have no idea how I will manage identifying each EditText and button with a unique ID, and thus, I have no idea how I would manage to grab the values of each EditText for saving to my database. Can someone advise me on what I need to do? Thank you.

Keeb13r
  • 235
  • 3
  • 8
  • 18

2 Answers2

2

If you have inflated a sub-layout, then you should now have a View object.

You can then call findViewById(R.id.edit_text_1) on that View — assuming that you supplied IDs in the sub-layout XML.

So long as you keep track of each of the parent Views, you can always use findViewById() on them. Or after inflation, if you really want you can set a new, globally-unique ID on each EditText using setId().

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • What if I have in my sub-layout multiple EditTexts next to each other, wrapped in a RelativeLayout, and I have layout attributes such as "layout_below", "layout_toRightOf"? Would I have to modify these attributes when I change the IDs of all the EditTexts? – Keeb13r Dec 24 '10 at 23:12
  • And is there no way I can set a string to be the element's ID? It seems absolutely counter-intuitive that "setId()" will only accept an int as an argument. – Keeb13r Dec 24 '10 at 23:20
  • You can set a *reference* to a string as the ID, but not an arbitrary string defined at runtime. But again, I don't think it's necessary to change IDs. So long as you keep track of the parent Views, which presumably you need to do anyway, you don't need to re-set the IDs of any of its children. – Christopher Orr Dec 26 '10 at 16:01
  • Instead of using data member variables to store these elements (like I am with static EditTexts), I'm trying to store them in a data member multidimensional array of Objects (the row being each instance of the sub-layout, and the columns being each instance of the sub-layouts elements). Do you think this is a good way of doing it? – Keeb13r Dec 27 '10 at 07:41
0

You can assign a view widget dynamically, and generate your own ID as it is assigned.

Button b = new Button(this);
b.setId(myId);

Then you can refer back to that widget.

findViewById(myId);
user432209
  • 20,007
  • 10
  • 56
  • 75
  • Let's say that I want to follow [this example](http://stackoverflow.com/questions/4299924/inflating-a-view-multiple-times-in-the-same-parent-when-a-button-is-clicked). The wrapper view has already been inflated with the sub-view when I click the add button. Now, how do I set the IDs of these new elements since, presumably, they have already been created by the inflater (thus making "Button b = new Button(this)" unnecessary)? Or how can I refer to them for data storage purposes? – Keeb13r Dec 20 '10 at 23:40