views:

33

answers:

1

I want to work dynamically therefore I want to bind text views dynamically I think an example would explain me the best

assuming I want to bind 7 image views i can do it like this :

 Country = (EditText)findViewById(R.id.CountryEditText);
 City = (EditText)findViewById(R.id.CityEditText);
 LivinigCreture = (EditText)findViewById(R.id.LivingCretureE);
 Nature =(EditText)findViewById(R.id.NatureEditText);
 Inanimate = (EditText)findViewById(R.id.InanimateEditText);
 KnowenPersonality = (EditText)findViewById(R.id.KnowenPersonalityEditText);
 Occupation = (EditText)findViewById(R.id.OccupationEditText);

but lets change 7 with NUMOFFILEDS as a final where i want to do the previous ?

   myImages = new ImageView [7];
   for (int i = 0; i<7;i++,????)
   myImages[i] = (ImageView)findViewById(R.id.initialImageView01);

notice : in my R file the R.id.initialImageView01 - R.id.initialImageView07 are not generate in a cont gap between them therefore I don't know how to make this architecture possible . and if there's a way can someone show me an example how to work dynmiclly (like using jsp on android combined way or something ?)

id its possiable to do so constant times is it possible to build an the same xml constant num of times like jsp does
thank u pep:)

A: 

You can store the IDs themselves in an array at the beginning of your Activity; that way you'll only need to write them once and you can index them afterwards.

Something like:

int[] initialImageViewIds = {
    R.id.CountryEditText,
    R.id.CityEditText,
    R.id.LivingCretureE,
    R.id.NatureEditText,
    R.id.InanimateEditText,
    R.id.KnowenPersonalityEditText,
    R.id.OccupationEditText
};

Then you can access them with:

myImages = new ImageView [7];
for (int i = 0; i<7;i++) {
    myImages[i] = (ImageView)findViewById(initialImageViewIds[i]);
}

If that's not enough and you really want to get the IDs dynamically, I suppose you can use reflection on the R.id class, possibly with something like R.id.getClass().getFields() and iterate on the fields to check if their names interest you. Check reference for the Class class, too.

Joubarc
yes but you know it feels like its not the real thing that is no real context driven binding.
yoav.str
Sorry, maybe I don't fully understand what you mean by context driven binding.One thing is, I don't think you absolutely need to let the system generate IDs for you - I don't know if it's a sound idea but I don't see any problems with you attributing the IDs manually in your XML layout with `android:id="12345"` or something.The other option I see is that you may want to create Views at runtime and add them to your parent view programatically, if for example you don't know in advance how many you'll add (for example TableRows in a TableLayout) - is that what you want?
Joubarc