1

当我从包中获取数据时,'if' 控件永远不会工作,请帮助我.. 我需要在'if' 块中比较包的数据,因为我必须根据数据更改 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("...");
    }
}
4

4 回答 4

2

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

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

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

if (get.equals("C")) {
    txtV.setText("...");
}
于 2012-07-17T13:18:14.707 回答
2

您可以使用

if (get.equals("A")) { //my code ...
于 2012-07-17T13:18:54.040 回答
0

您不能通过 比较字符串==,因为这只会检查对象身份,而具有相同内容(例如A)的两个字符串可能是单独的对象。改用equals()

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

请注意比较中的不同顺序。这可以防止NullPointerExceptionsifget应该为空。

这是一个很好的解释。

于 2012-07-17T13:19:10.870 回答
-1
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("...");
    }
  }
}
于 2012-07-17T13:25:09.230 回答