7

I have a form with many EditTexts, and when I press a certain button, I need to retrieve all these controls and put them into a HashMap so the key is the name (key1 int the following code)

<EditText android:id="@+id/key1" 
        style="@style/keys" /> 

and the value, whatever text the user enters.

My question is, how can I retrieve the name of the EditText for the Hashmap's keys ? getId() returns a number.

Thanks

xain
  • 13,159
  • 17
  • 75
  • 119
  • 1
    What do you plan to use that name for? Methods like `findViewById()` accept the ID `int` as a parameter. – dave.c Jan 31 '11 at 14:16
  • Whose findViewById ? I tried the ones from the EditText, the ScrollView and form the Layout and all of them return a reference to EditText (for instance android.widget.EditText@46734038). – xain Jan 31 '11 at 15:37

3 Answers3

3

Android generates a handle for that View in R.java whenever you build your project. For instance, once you build you can access your EditText by calling R.id.key1. You don't have to store the ids anywhere because you can access the id directly at any time in your code. With this id you can call findViewById() as dave.c mentions to get whatever view you need from your XML.

McStretch
  • 20,495
  • 2
  • 35
  • 40
  • Thanks for you reply. What if I need to send the pair key/value to a web service ? In such case I need the key to be always the same, and preferably a "readable" one, like the way it's done in web forms. – xain Jan 31 '11 at 14:24
  • 1
    @Xain: If you need it to always be the same then you need to make sure you don't change it. You as the developer have full control over the id. That being said I would avoid using the id for a key to a webservice. Instead I would use something like a public static String grouped with other web service keys - perhaps in a static class like WebServiceKeys. The view ids aren't meant for this purpose. – McStretch Jan 31 '11 at 14:27
  • Well, you don't have **full** control over the ID as Android generates the `int` id for you, and these can change as you add and remove items. I agree that @xain should maintain his own keys for these inputs. – dave.c Jan 31 '11 at 14:35
  • @dave.c: That's true. I figured he wanted the String representation of the id though, because he doesn't like what getId() returns. – McStretch Jan 31 '11 at 14:40
  • That's right and a good place to keep the same id strings is the xml file. Now I just need to retrieve them from the Activity. – xain Jan 31 '11 at 14:44
  • @Xain: It's a good place to keep the related id Strings to be used for the purpose of interacting with your Views for a given Activity, not for keeping webservice key/value names. – McStretch Jan 31 '11 at 14:48
  • And what do you suggest to use to map the xml's id to a class with the actual ws parameter names with other than the "string ids" if the generated (int) ids change when the app is recreated? – xain Jan 31 '11 at 14:58
  • @Xain: I wouldn't suggest mapping them at all. The XML view ids might have some similarity to the webservice names, but they're being used for different purposes. For instance, if you have an EditText for a "last name" you might call it last_name_edit in your XML, which you'll later reference in your code to grab the value, edit it, etc. When you're ready to pass the value to the webservice you might have a key such as lastName that you pass to the webservice (whatever it expects). – McStretch Jan 31 '11 at 15:06
2

I finally solved it using android:tag and getTag()

xain
  • 13,159
  • 17
  • 75
  • 119
0

Actually, getId() is the integer value of the component IDs, it's the same as described in the R generated class.

You need the actual name as you wrote? If you need just a reference the int value is enough.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167