FindViewById where ID is dynamic string

前端 未结 3 1720
甜味超标
甜味超标 2020-12-16 13:58

I have a table of 40 squares and each has an ID. When I\'m passing Bundle from another activity I need to extract Note, Color and ID from that bundle. And then the app will

相关标签:
3条回答
  • 2020-12-16 14:30

    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.

    0 讨论(0)
  • 2020-12-16 14:38

    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.

    0 讨论(0)
  • 2020-12-16 14:45

    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.

    0 讨论(0)
提交回复
热议问题