Issue
As an exercise, I am building a calendar. Below is a small segment of my code. I have a class MyCalendar that extends Activity.
This Activity will be linked to an XML file whose layout includes seven TextViews with ids R.id.day0 thru R.id.day6.
If converted from String to int, will the Activity's findViewById() method recognize the int version of these ids?
String Days [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
TextView dayViews [];
private void populateViews (){
for (int counter = 0; counter < Days.length; counter++){
int ID = Integer.parseInt("R.id.day"+counter);
dayViews [counter] = (TextView) findViewById(ID);
dayViews[counter].setText(Days[counter]);
}
Solution
I have not tried it myself. but this answer https://stackoverflow.com/a/11595723/1514861 suggest you can do it like this:
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
EDIT: I think you would also need to replace "string" with "id"
Answered By - Ivo Beckers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.