1. Check out our app, Forums for Android! Download and leave feedback here!

String to CharSequence

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

  1. __Jon__

    __Jon__ Member
    Thread Starter
    15

    Aug 16, 2010
    15
    0
    15
    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
    Thread Starter
    15

    Aug 16, 2010
    15
    0
    15
    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
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    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
    Thread Starter
    15

    Aug 16, 2010
    15
    0
    15
    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
    38

    Apr 23, 2010
    125
    17
    38
    Software Engineer
    UK
    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
    6

    Sep 18, 2009
    4
    1
    6
    Android developer
    Hyderabad
    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
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    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...