0

Is there a way to change a reference to an ID in the Android manifest using a variable?

As in:

for(int counter6=1;counter6 <= 12; counter6++)
                value = bundle.getString("value"+counter6);
                TextView text1 = (TextView) findViewById(R.id.textView+counter6);
                text1.setText(value);

Is it possible to have the counter6 variable used in the ID directory, so the for loop can loops through all the different text view making each one text1 respectively then setting their text to the string value?

Its not really a problem if it cant work this way it just means more lines of code to write.

user2682570
  • 94
  • 1
  • 2
  • 10
  • its really not though? – user2682570 Sep 12 '13 at 15:15
  • 1
    It is because you can't assume that `R.id.foo1` +1 is `R.id.foo2`. You'll have to use the name in that case. – zapl Sep 12 '13 at 15:18
  • 1
    @user2682570 so you're using something like : `findViewById(getResources().getIdentifier("textView" + counter6, "id", getPackageName()))` or not? you can also do smthing like `int[] theids = new int[] {0, R.id.textView1, R.id.textView2, ... , R.id.textView12}` and then `findViewById(theids[counter6])` – Selvin Sep 12 '13 at 15:20
  • umm no? The code that i thought could work is in my question. is that what i should use to to have it loop through? – user2682570 Sep 12 '13 at 15:23
  • ok nice one :D cheers – user2682570 Sep 12 '13 at 15:24
  • @zapl - not exactly - the ID's are fixed at compile time, so if one needs them to be in order that can be accomplished, but of course it could be broken again if the sources are modified by someone who doesn't keep that need in mind, so relying on it isn't usually a good idea. – Chris Stratton Sep 12 '13 at 15:37
  • @ChrisStratton you are right, http://stackoverflow.com/questions/9348614/what-is-the-use-of-the-res-values-public-xml-file-on-android would allow doing that. You ruined my attempt to simplify things :) – zapl Sep 12 '13 at 15:48
  • Can you explain a bit more about the use case? I cannot think about an example where this loop would be useful. You either set the text in the XML or you can set them programmatically - but doing that in a loop like the one in the question would set all `TextView` to the same value? – jboi Sep 12 '13 at 18:10
  • its just to constantly update the text with new values that are fed in by my app reading a text file :) don't worry though ill just do it the long way :L – user2682570 Sep 13 '13 at 08:41

2 Answers2

0

You can't really make a loop on the id and increment it as it is generated but you can make an array of references and by getting that array find each TextView and update the text:

<array name="array_text_views">
       <item>@id/text_view_1</item>
       <item>@id/text_view_2</item>
       <item>@id/text_view_3</item>
<array>

In your code, something like that:

ArrayList<TextView> myTextViews = new ArrayList<TextView>();
TypedArray ar = context.getResources().obtainTypedArray(R.array.array_text_views);
int len = ar.length();
for (int i = 0; i < len; i++){
    myTextViews.add(findById(ar[i]));
} 
ar.recycle(); 
galex
  • 3,279
  • 2
  • 34
  • 46
  • The ID's are ultimately just numbers, so you can in fact iterate over them, but you would have to either insure they were a contiguous sequence or safely handle gaps. – Chris Stratton Sep 12 '13 at 15:35
0

I would usually just put a small int[] array of Ids into the code somewhere. If you have a lot of them, consider creating them programmatically (layout.addView(new TextView(..).

For example if you want to start an Activity and tell it what strings to display via the Extras Bundle you can put them directly as an array.

void startOther(String[] texts) {
    Intent i = new Intent( /* ... */);
    i.putExtra("texts", texts);
    // start via intent
}

Now inside that Activity I would put the ids as a "constant".

// hardcoded array of R.ids
private static final int[] TEXT_IDS = {
    R.id.text1,
    R.id.text2,
    // ...
};

And then use both the Bundle and the id Array for example like this:

// a List of TextViews used within this Activity instance
private List<TextView> mTextViews = new ArrayList<TextView>(TEXT_IDS.length);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.something);

    // find all TextViews & add them to the List
    for (int id : TEXT_IDS) {
        mTextViews.add((TextView)findViewById(id));
    }

    // set their values based on Bundle
    String[] stringArray = savedInstanceState.getStringArray("texts");
    for (int i = 0; i < mTextViews.size() && i < stringArray.length; i++) {
        mTextViews.get(i).setText(stringArray[i]);
    }
}
zapl
  • 63,179
  • 10
  • 123
  • 154