如果,Else,setText有问题

时间:2014-06-15 08:30:49

标签: java settext

我目前正在尝试处理结果,如果收到输入并遇到一些麻烦,我希望能够将文本设置为特定短语,如果没有结果,并希望能够拥有如果有,输入显示正确。我目前正在尝试这个:

String s = scanningResult.getContents();
    if (s == null){ 
        s="Location Is here,Time is here,Cost is here";
    }
    else {
        s=s;
    }
    String [] s2 = s.split(",");

    TextView location = (TextView)findViewById(R.id.LocationResult);
    location.setText(s2[0]);


    TextView time = (TextView)findViewById(R.id.TimeResult);
    if (s2[1] == "Time is here"){ 
        time.setText("Time is here");
    }
    else {
        time.setText(s2[1]+" Seconds");
    }


    TextView cost = (TextView)findViewById(R.id.CostResult);
    if (s2[2] == "Cost is here"){ 
        cost.setText("Cost is here");
    }
    else {
        cost.setText("$"+s2[2]);
    }

}

我遇到的问题是,在null scanningResult上,timecost两个值将显示为Time is here Seconds$Cost is here。我不明白为什么。

1 个答案:

答案 0 :(得分:4)

Java中的

使用equals方法来比较String

if (s2[1].equals("Time is here")){ }