How to make findViewById (R.id. >> StringVarHere <<)?

I searched and worked on it for a long time - no luck. (Should it be easy? Thanks for the help.)

Trying to get / set a screen full of EditTexts text, but not with the usual, more rigid way:

 ... findViewById (R.id.SomeTextWidgetId) ; 

Instead, I'm trying to find a way to reuse it through a variable containing the name (String) name_of_widget.

In the psuedo code:

findViewById (R.id. → StringVarHere <<); // how to do this?

I also tried this findViewById method, but it did not work (!?)

 //// given: static final String FIELD_TV_FEE = "TextViewFee" ; static final String FIELD_TV_FOO = "TextViewFoo" ; static final String FIELD_TV_FUM = "TextViewFum" ; //// and some arbitrary number more of similar fields static final String [] ALL_FIELDS = { FIELD_TV_FEE , FIELD_TV_FOO , FIELD_TV_FUM // ... } ; //// ... //// this part works int ResourceID; String stringVarHere = FIELD_TV_FEE; //// outputs a correct id, say '0x7f05000f' as in R.id.xxx below ResourceID = context .getResources() .getIdentifier ( stringVarHere, "id", context .getApplicationInfo() .packageName ) ; Log.d ("MyClass" , "RESID = " + Integer.toHexString(ResourceID) ) ; /* * that where I'm stuck ^^^ ... how do I do: */ String field_name ; for ( field_name : ALL_FIELDS ) { (EditText) SomethingLike_a_findViewById(field_name).setText ("Hello Wurld") ; } 

I tried .setId ...

 //// details <!-- excerpt from working xml layout --> <EditText android:id="@+id/TextViewFee" android:inputType="text" android:layout ... etc ... /> <EditText android:id="@+id/TextViewFoo" android:inputType="text" android:layout ... etc ... /> <EditText android:id="@+id/TextViewFum" android:inputType="text" android:layout ... etc ... /> 

As expected, there is something like this in the gen'ed R file:

 // ... public static final class id { public static final int TextViewFee=0x7f05000f; public static final int TextViewFum=0x7f05001c; public static final int TextViewFoo=0x7f05001d; // ... etc 

Yes, thanks - it makes sense to do this in your work. I tried to keep it from being too cumbersome code. Here's what I'm doing right now, based on your suggestions and AC's suggestions. The goal is to return the entire text of the form fields in one line []. (I know that I could go over all the fields too.)

What do you all think of this below - seems very similar to your suggestion, madlymad? I wonder if this is a bad design approach?

 public class FoodBar { private Activity activity; private Context ctx; public FoodBar ( Activity _activity ) { this.activity = _activity; this.ctx = this.activity.getApplicationContext() ; } public String[] getTextFromAllEditTexts () { // the UI views int res_id = 0; int i = 0; String [] retValues = new String [MyClassName.ALL_FIELDS_LENGTH] ; for (String field : MyClassName.ALL_FIELDS_ALL_VEHICLES) { res_id = this.ctx.getResources() .getIdentifier ( field, "id", this.ctx.getPackageName() ); ((EditText) this.activity .findViewById (res_id)) .setText( "Meat and Potatoes" ) ; // redundant - get it right back to make sure it really went in ! retVal[i++] = ((EditText) this.activity .findViewById (res_id)) .getText().toString() ; } return retVal; } // end func } // end class 

Then from the Activity class, simply:

 String [] theFields = null; FoodBar = new FoodBar (this); try { theFields = FoodBar.getTextFromAllEditTexts (); } catch (Exception e) { Log.d ("OOPS", "There a big mess in the Foodbar: " + e.toString() ); } 
+6
source share
3 answers

How can you do this (as I understand it, how are you trying):

It may be in inactive (YourClassname.java):

 public static int getMyId(Context context, String field) { return context.getResources().getIdentifier (field, "id", context.getPackageName()); } 

in the Activity class:

 for ( String field_name : YourClassname.ALL_FIELDS ) { int resid = YourClassname.getMyId(context, field_name); if(resid != 0) { // 0 = not found EditText et = (EditText) findViewById(resid); if (et != null) { et .setText ("Hello Wurld") ; } } } 

But I think that the code is better in the activity class, for example:

 String packageName = getPackageName(); Resources res = getResources(); for ( String field_name : YourClassname.ALL_FIELDS ) { int resid = res.getIdentifier (field_name, "id", packageName); if(resid != 0) {// 0 = not found EditText et = (EditText) findViewById(resid); if (et != null) { et .setText ("Hello Wurld") ; } } } 
+3
source

AC suggested something like:

 res_id = getResources().getIdentifier (field, "id", getPackageName()); ((EditText)findViewById (res_id)).setText("NoLongerFubar"); 

this work works - when I tried it offline on a test setup. Thank you Still not sure what exploded, but I suspect that context or resource objects are unavailable.

+2
source

Note that variable names (e.g. R.id.some_id ) are only available at compile time and cannot be accessed from a String value at run time. Since these identifiers are declared as int s, you can use int[] or List<Integer> to store identifiers. Depending on how dynamic your layout is and what you do with the views in it, you can even just create views at runtime and store an array or list of them without using any identifiers at all.

+1
source

All Articles