字符串转换发布

时间:2017-09-18 07:22:18

标签: java android string format charsequence

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

基本上:我从String.xml保存字符串(文本)。例如:

"value of %s: %s"

我在参数中保存参数的引用。 这些引用可以是Strings,SpannableStrings或dpzahl's。

但是在准备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”而BigDecimal表达式为4则输出为

输出:

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字符串应该是“testvalue:4的值”,但它是“testvalue的值:4.000000000”

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

ps:当我将SpannableString实现到formater时出现问题,在此之前,该行

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

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

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

pp:使用

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

无济于事。

1 个答案:

答案 0 :(得分:-1)

改变

if(ooo instanceof SpannableString)

else if(ooo instanceof SpannableString)