字符串转换问题

String converting issure

我遇到了字符串转换问题。 我正在尝试实现一个系统,在该系统中,数字 (BigDecimal) 在更新时会打印到屏幕上。 因此我创建了 BigDecimal (dpzahl) 的子类来检测更新的数字并向 ui.

发送更新请求

所以基本上:我从 String.xml 中保存字符串(文本)。示例:

"value of %s: %s"

并且我将参数的引用保存在参数中。 这些引用可以是字符串、SpannableStrings 或 dpzahl 的。

但是在准备 string.format 我遇到了一个问题:

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
    }
});

如果我输入 SpannableString "testvalue" 和 4 的 BigDecimal 表达式,输出为

输出:

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]

所以 TextView 字符串应该是 "value of testvalue: 4" 但它是 "value of testvalue: 4.000000000"

那么为什么 ARGS[1] 的值首先是字符串“4”的值,然后是值“4.000000000”,然后再传递给格式化程序?

ps:当我将 SpannableString 实现到格式化程序时出现问题,在此之前,行

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

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

一切正常。但是 SpannableString 不扩展字符串,所以我需要下一个上层最低公分母,即 CharSequence。

pp:使用

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

没有帮助。

更改

if(ooo instanceof SpannableString)

else if(ooo instanceof SpannableString)