How to manipulate with incremental names of fields

2019-08-06 14:22发布

问题:

How to manipulate with incremental names of fields? For example:

 myBitmapField1
 myBitmapField2
 myBitmapField3
 ...

Is a way to add them to FieldManager in a way like this or similar:

int i = 0;
while (i < 1000)
 {
 i = i + 1;
 if (myCounter == i)
 myVerticalFieldManager.add(_myBitmapField[i]);
 }
   ...

There is a way to manipulate with incremental names of file names like this:

 myBitmapField.setBitmap(Bitmap.getBitmapResource("a" + myCounter + ".png"));

But I need to manipulate with names of fields ! How could I do that?

回答1:

Ideally, don't - use an array (or possibly a List) instead. If you have to use names, create an appropriate Map (e.g. a HashMap) from names to values.

While you can access fields using reflection - at least in "normal" Java - it would be a poor design here. You've got multiple related values, so use a collection... it's as simple as that.



回答2:

If I understand correctly, you want to access the fields in a manager by name. You can instead access them by field position. Perhaps that will do for what you need.