• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps String to CharSequence

__Jon__

Newbie
I'm looking for a way to convert a string to a charsequence.
My code is:
Code:
public void randText(int r){
  
   r++; //necessary increment
 
   String rs = Integer.toString(r); //random string
 
   String qs ="R.string.q"+rs;//question string. 
 
   CharSequence cs = qs.subSequence(0, 12);
 
   newtv.setText(cs); //this is a textview.

The random int r is created outside the method. This method gives a random string, from my resource file. All of the strings have names q1,q2,q3,... There will be 100+ such strings.

This doesnt work at all. the setText(cs) line fails. Is there any way to convert string -> charsequence? Or to have it read as a resource file and not a string?
 
Well I got it to work, kind of.
I made the mistake of counting 12 characters and inputting 12 into the subsequence method. I should have put 11.

Now the method works, but for some reason. If r=0 then the resource is R.string.q1. But the output is "R.string.q1". If r=1, then the compiler reads it as a resource, as I intended.

I have no idea how or why it is discriminating so wierdly between the resources q1 and q2.
 
Thanks Blundell, but actually I did it all completely wrong. It didn't make sense at all. And the part that I thought worked was because something else was happening.

What I wanted to do (which seems to be impossible) is change a string to a filepath
"R.string.q1" ---> R.string.q1



Actually what I needed to do was just make a string array and string-array in the XML file, then I could randomly choose elements of the array at will. Easy-peasy. But I tried to do something that made no sense.
 
Yeah what you were trying isn't possible it Java. But is in AS3 or Groovy or PHP even so you just got a little confused ;-) GL with the rest!
 
Hi,
I albe to convert String /StringBuffer to CharSequence But I am unable to convert String[].
Is there any way to convert String[] to CharSequence[] ?
 
I am very confused on why you would ever need to do this. CharSequence is an interface, and String implements this interface, so you don't need to convert. Just pass the string wherever an instance of CharSequence is required.
 
Back
Top Bottom