String to CharSequence


Last Updated:

  1. __Jon__

    __Jon__ Member This Topic's Starter

    Joined:
    Aug 16, 2010
    Messages:
    15
    Likes Received:
    0
    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?
     

    Advertisement
  2. __Jon__

    __Jon__ Member This Topic's Starter

    Joined:
    Aug 16, 2010
    Messages:
    15
    Likes Received:
    0
    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

    Joined:
    Apr 23, 2010
    Messages:
    125
    Likes Received:
    17
    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__ Member This Topic's Starter

    Joined:
    Aug 16, 2010
    Messages:
    15
    Likes Received:
    0
    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

    Joined:
    Apr 23, 2010
    Messages:
    125
    Likes Received:
    17
    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 New Member

    Joined:
    Sep 18, 2009
    Messages:
    4
    Likes Received:
    1
    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 Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.

Share This Page

Loading...