开发者

android dynamical binding

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);
 Occupatio开发者_如何学编程n = (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:)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜