2013-03-01 28 views
-1

我使用下面的代碼來獲取selectediem文本,我喜歡根據所選內容顯示某些內容,但它不符合任何線索。Android微調選定項目與文本不匹配

Spinner mlogin_store; 
mlogin_store = (Spinner) findViewById(R.id.spinlogin_store); 
String Text = mlogin_store.getSelectedItem().toString().trim(); 

Log.d("click",Text); //I can see the "Abc" in LogCat. but it doesn't match the string below. 
if (Text=="Abc"){ //first block 
//Do something 
}else{ 
//do something else} 

回答

0

不要==比較字符串。 總是使用字符串函數進行比較,例如。

if (Text.equalsIgnoreCase("Abc")) 
{ 
    //first block 
    //Do something 
} 
else 
{ 
    //do something else} 
} 

equalsIgnoreCase功能指定的字符串進行比較,這個字符串忽略 字符的情況下,如果它們相等返回true。

+0

ha ...後通知,即時通訊非常新的Java。 :) 謝謝。無論如何 – tsohtan 2013-03-01 07:06:47

0

使用Text.equals( 「ABC」)

現在您的代碼

Spinner mlogin_store; 
    mlogin_store = (Spinner) findViewById(R.id.spinlogin_store); 
    String Text = mlogin_store.getSelectedItem().toString().trim(); 

    Log.d("click",Text); //I can see the "Abc" in LogCat. but it doesn't match the string below. 
    if (Text.equals("Abc")){ //first block 
    //Do something 
    }else{ 
     //do something else 
}