Java Question String to Charsequence - What do i need to take care of

DonManfred

Expert
Licensed User
Longtime User
Asuming i want to change a lib which uses Strings now and i want to change the lib to support Charsequences.

What do i need to take care of? Any known tripping hazards?

Do i simply change all References from String to Charsequence?
 

corwin42

Expert
Licensed User
Longtime User
In all setters and getters where the library supports CharSequence use also CharSequence for the parameters in your wrapper.

So for AppCompat or DesignSupport I changed some of the wrappers/getters like this:
B4X:
     /**
     * Set the Tab text.
     */
    public void SetTabText(int Index, CharSequence Text) {
        getObject().getTabAt(Index).setText(Text);
    }

    /**
     * Get the Tab text.
     */
    public CharSequence GetTabText(int Index) {
        return getObject().getTabAt(Index).getText();
    }
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess that it is obvious but remember that it only makes sense to change String to CharSequence when the underlying API (the one that your library calls) supports CharSequence.

I must say that I haven't changed any existing getter that returns String to CharSequence.
For example TextView.Text property:
B4X:
public String getText() {
     return String.valueOf(getObject().getText());
   }
   public void setText(CharSequence Text) {
     getObject().setText(Text);
   }

I expect it to be safe however as these methods are used by many developers and in all kinds of strange ways (and changing the getter is less important) I decided to leave it like this for now.
 
Top