FindViewById where ID is dynamic string

我们两清 提交于 2019-11-29 03:52:42

You can get an identifier from a string by using:

notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));

Where VIEW_NAME is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id to the appropriate type.

Assuming you're passing in the actual string "R.id.passed_id", I would recommend passing in the integer value instead. Thus, simply pass in R.id.passed_id.

All values in the R file of an application are simply integers that are mapped to resource locations. The findById() methods are only looking for integers, so if you start the Activity and pass in the integer value of the resource you want, it will find it.

Note: This will only work if the resources are within the same application. R files remain constant for an application, but they can change from one app to another even if they have the same name. If, however, the two activities fall under the same application package, it will work.

Man I'm not understating what exactly you want to do. Id is the id off an android widget then on function initialize() you should cast string to int this way: Integer.ValueOf(Id) Hope this is what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!