Android Question CharSequence vs. CSBuilder in parameters?

Discussion in 'Android Questions' started by Dave O, Nov 9, 2018.

  1. Dave O

    Dave O Active Member Licensed User

    If we want to include support for CharSequence in our classes and libraries, should we be changing external-facing String parameters (and any related internal ones) to CSBuilder?

    (If I understand correctly, we can't reference CharSequence directly.)
     
  2. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    As I understand, we can replace String to CharSequence as parameters in own library without changing other code.

    CharSequence is an interface, not a concrete class. And implemented in a lot of classes (including String)
    AlteredCharSequence, CharBuffer, Editable, GetChars, PrecomputedText, Spannable, SpannableString, SpannableStringBuilder (CSBuilder), Spanned, SpannedString, String, StringBuffer, StringBuilder

    Small sample. In java code input and output parameters declared as CharSequence.
    We can use strings as input-output (first call).
    We can use CSBuilder or another classes as input-output (second call).

    Code:
    Sub Activity_Resume

        
    Dim jo As JavaObject
        jo.InitializeContext

        
    Dim strInput1 As String = "Test for String"   
        
    Dim strOutput1 As String = jo.RunMethod ("test"Array (strInput1))
        
    Log (strOutput1)

        
    Dim strInput2 As CSBuilder
        strInput2.Initialize.Append (
    "Test for CSBuilder")
        
    Dim strOutput2 As CSBuilder
        strOutput2.Initialize.Append (jo.RunMethod (
    "test"Array (strInput2)))
        
    Log (strOutput2)

    End Sub

    #If Java

    import java.lang.CharSequence;

    public CharSequence test (CharSequence parameter) {
       return parameter.toString () + " *** Added by test";
    }
    #End If
     
    Last edited: Nov 11, 2018
  3. Erel

    Erel Administrator Staff Member Licensed User

    You should use Object.

    Example from xCustomListView:
    Code:
    Public Sub AddTextItem(Text As Object, Value As Object)
     
    Dave O likes this.
  4. Dave O

    Dave O Active Member Licensed User

    Thanks. So, in the depths of my class, when I need to do something with the argument, I'll need to cast it to csBuilder?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    In most cases you don't need to cast it.

    Just use it:
    Code:
    Label1.Text = Text
    If you do need to cast it then you must check its type as it can be either CSBuilder or String.
     
    Dave O likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice