Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random generation

 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to select IDs randomly from a string and then use setText function, but it is not working. :/
Please see the code below:

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TellTheDetails. What does"not working" mean?
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:TellTheDetails. What does"not working" mean?


ADT says: "The method setText(String) is undefined for the type String"
 
Jamie Coleshill
Greenhorn
Posts: 8
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing is where are you declaring your String[] mezok? The second thing is that you can not do .setText("") on a string or array of Strings, you must use the = sign as follows because as the ADT say's "The method setText(String) is undefined for the type String".

 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie Coleshill wrote:First thing is where are you declaring your String[] mezok? The second thing is that you can not do .setText("") on a string or array of Strings, you must use the = sign as follows because as the ADT say's "The method setText(String) is undefined for the type String".



I have declared in onCreate. I would like to use this string as a textview id. How can I convert a string to a resource ID?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kovacs Akos wrote:I would like to use this string as a textview id. How can I convert a string to a resource ID?

You can't, the ids are all integers. You can sort of work around it by using reflection to load the .R class for your application and searching for the variable name with the same name and getting its value. But it is slow and complex, and fragile. I would suggest a different approach - perhaps a Map<String,Integer> that you pre-fill with the Strings you want to the ID it represents.
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found entries like this for all the variables: "0x7f090001" ...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks about right... That being an integer represented in hex.
 
doug mccann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kovacs Akos wrote:


It look like you may need something like for your index as well as using =
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doug mccann wrote:
Kovacs Akos wrote:


It look like you may need something like for your index as well as using =


It says:

"Multiple markers at this line
- Syntax error on token ";", delete this token
- Syntax error on token "%", invalid "
 
doug mccann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doug mccann wrote:


Thanks, this code seems correct. Can you tell me please what is the essence of this solution or how is it working?
 
doug mccann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need a number that is between 0 and the array size -1. "%" returns the remainder as in 7 % 5 -> 2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic