繁体   English   中英

Android Bundle与String的比较

[英]Android Bundle Comparison with String

当我从bundle获取数据时,'if'控件永远不会工作,请帮助我..我需要在'if'块中比较bundle的数据,因为我必须根据数据更改textview。

result = getIntent().getExtras();
String get = result.getString("secilen");

if(number == 0) {
    imgView.setImageResource( R.drawable.tas );

    //txtV.setText(get);

    if (get == "A"){ // if even "A" come never read if block
        txtV.setText("...");
    }

    if (get == "B"){
        txtV.setText("...");
    }

    if (get == "C") {
        txtV.setText("...");
    }
}

使用equals而不是==来比较字符串:

if (get.equals("A")){ 
    txtV.setText("...");
}

if (get.equals("B")){
    txtV.setText("...");
}

if (get.equals("C")) {
    txtV.setText("...");
}

您可以使用

if (get.equals("A")) { //my code ...

您无法通过==比较字符串,因为这只会检查对象标识,而具有相同内容的两个字符串(例如A )可能是单独的对象。 使用equals()代替:

if ("A".equals(get)) {
        txtV.setText("...");
}

请注意比较中的不同顺序。 如果get应为null,则会阻止NullPointerExceptions

这是一个很好的解释。

result = getIntent().getExtras();
if(result!=null){
    String get = result.getString("secilen");
    if(number == 0){
    imgView.setImageResource( R.drawable.tas );


    if (get.equals("A"))
        txtV.setText("...");
    }

    else if (get.equals("B")){
        txtV.setText("...");
    }

    else if (get.equals("C")) {
        txtV.setText("...");
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM