1. Are you ready for the Galaxy S20? Here is everything we know so far!

String to CharSequence

Discussion in 'Android Development' started by __Jon__, Aug 17, 2010.

  1. __Jon__

    __Jon__ Newbie
    Thread Starter

    I'm looking for a way to convert a string to a charsequence.
    My code is:
    Code (Text):
    1. public void randText(int r){
    2.  
    3.    r++; //necessary increment
    4.  
    5.    String rs = Integer.toString(r); //random string
    6.  
    7.    String qs ="R.string.q"+rs;//question string.
    8.  
    9.    CharSequence cs = qs.subSequence(0, 12);
    10.  
    11.    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?
     


  2. __Jon__

    __Jon__ Newbie
    Thread Starter

    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.
     
  3. blundell

    blundell Well-Known Member

    Is this
    String qs ="R.string.q"+rs;//question string.

    Supposed to be:
    Code (Text):
    1.  
    2. String qs = getString(R.string.q)+rs;//question string.
    3.  
    Context | getString()
     
  4. __Jon__

    __Jon__ Newbie
    Thread Starter

    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.
     
  5. blundell

    blundell Well-Known Member

    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!
     
  6. jettimadhu

    jettimadhu Lurker

    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[] ?
     
  7. jonbonazza

    jonbonazza Android Expert

    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.
     
    alostpacket likes this.
Loading...
Similar Threads - String CharSequence
  1. Stokestack
    Replies:
    6
    Views:
    523
  2. Cinemato
    Replies:
    0
    Views:
    373
  3. Guitarist
    Replies:
    0
    Views:
    553
  4. ElChief
    Replies:
    1
    Views:
    222
  5. Prashant Sontale
    Replies:
    0
    Views:
    210
  6. EAkio
    Replies:
    5
    Views:
    714
  7. Federico Desia
    Replies:
    8
    Views:
    511
  8. Vora Ravi
    Replies:
    0
    Views:
    322
  9. Federico Desia
    Replies:
    3
    Views:
    752
  10. MoodyBlues
    Replies:
    28
    Views:
    1,524

Share This Page

Loading...