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() ); }