【问题标题】:String converting issure字符串转换问题
【发布时间】:2017-09-18 07:22:17
【问题描述】:

我遇到了字符串转换的问题。 我正在尝试实现一个系统,其中数字(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" 和 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 实现到格式化程序时出现问题,在此之前,行

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

曾经

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

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

pp:使用

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

没有帮助。

【问题讨论】:

    标签: java android string format charsequence


    【解决方案1】:

    改变

    if(ooo instanceof SpannableString)
    

    else if(ooo instanceof SpannableString)
    

    【讨论】:

    • 你是完全正确的。没有涉及两个语句的 else 。首先执行正确的语句,然后下一个 if 会将 dpzahl 对象与 SpannableString 进行比较,因为它是 false,所以参数设置为默认的 .tostring() 方法。
    • 那不是我的投票,因为我不能投票,直到一些声誉左右,我试图投票
    猜你喜欢
    • 2017-07-12
    • 2019-07-25
    • 2016-05-26
    • 1970-01-01
    • 2013-08-22
    • 2013-02-15
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多