0

ive got a problem with string conversion. I am trying to implement a system where numbers (BigDecimal) are printed to the screen when they are updated. Therefore i created a subclass of BigDecimal (dpzahl) to detect the updated number and send a updaterequest to the ui.

So basically: i save the string (text) from String.xml. example:

"value of %s: %s"

and i save the references of the arguments in argument. these references can be Strings, SpannableStrings or dpzahl's.

But while preparing the string.format ive got a problem:

Log.d(MainActivity.LOGTAG,"update request is executed");
final Object[] ARGS = new CharSequence[argumente.size()]; //to be put into the formater
int i = 0;
for(Object ooo: arguments) { // private ArrayList<Object> arguments; is a class variable
    if (ooo instanceof dpzahl) { // dpzahl extends BigDecimal to get the number i want to format
        ARGS[i] = haupt.format_bigies(((dpzahl) ooo).get()); //get the formated string
        Log.d(MainActivity.LOGTAG,"number print:"+ARGS[i].toString());
    }if(ooo instanceof SpannableString){ //some other arguments i may need in the string.format argument list
        ARGS[i] = ((SpannableString)ooo);
    }else{ //for any other object, mostly Strings
        ARGS[i] = ooo.toString();
    }
    if (ooo instanceof dpzahl) { //only for debugprint
        Log.d(MainActivity.LOGTAG,"loopvalue dpzahl:"+ARGS[i].toString());
    }
    Log.d(MainActivity.LOGTAG,"loopvalue:"+ARGS[i].toString());
    i++;
}
for(Object ooo: ARGS) { //only for debugprint
    if(ooo instanceof String){
        Log.d(MainActivity.LOGTAG, "againarg Stirng:" + ((String)ooo));
    }else if(ooo instanceof SpannableString) {
        Log.d(MainActivity.LOGTAG, "againarg SpannableString:" + ((SpannableString)ooo).toString());
    }
    Log.d(MainActivity.LOGTAG, "againarg Object:" + ooo.toString());
}

sicht.post(new Runnable() {
    @Override
    public void run() {
        Log.d(MainActivity.LOGTAG,"allargs:"+ Arrays.toString(ARGS));
        view.setText(SpanFormatter.format(text, ARGS));//Copyright © 2014 George T. Steel, replace string.format for SpannableString
        view.invalidate();//the linked TextView
    }
});

If i put in the SpannableString "testvalue" and the BigDecimal expression for 4 the output is

output:

update request is executed
loopvalue:testvalue 
formated BigDecimal:4
number print:4
loopvalue dpzahl:4.000000000
loopvalue:4.000000000
againarg SpannableString:testvalue 
againarg Object:testvalue 
againarg Stirng:4.000000000
againarg Object:4.000000000
allargs:[testvalue , 4.000000000]

so the TextView String should be "value of testvalue: 4" but it is "value of testvalue: 4.000000000"

So why have the Value of ARGS[1] first the value of String "4" and later the value "4.000000000" before it is passed to the formater?

ps: the problem appears when i implemented the SpannableString to the formater, before this, the line

final Object[] ARGS = new CharSequence[argumente.size()];

was

final Object[] ARGS = new String[argumente.size()];

and all work fine. But SpannableString does not extend Strings so i need the next upper lowest common denominator which is CharSequence.

pp: using

final Object[] ARGS = new Object[argumente.size()]; 

does not help.

Piea
  • 3
  • 1

1 Answers1

-1

Change

if(ooo instanceof SpannableString)

to

else if(ooo instanceof SpannableString)
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • you are completely right. Without the else where two statements involved. First the correct statement is executet but then the next if will compare the dpzahl object with SpannableString and since it is false the argument is set to the default .tostring() method. – Piea Sep 18 '17 at 13:27
  • that was not my vote since i cant vote until some reputation or so, ive tryed to upvote – Piea Sep 18 '17 at 19:18