简体   繁体   中英

How to Use String as a ResourceID in Android

I've several TextView s or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.

Simply I want to iterate ids by using pre-string values.

Psuedo example:

String pre_value = "textView";

for(int i = 0; i < size; i++) {

    String usable_resource_id = pre_value + Integer.toString(pre_value);
    // So how to use these id like R.id.textView1
    // Cast or something similar

}

Any suggestions?

You can use Resources#getIdentifier() to get the identifier from a string.

But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM