1

The 'if' controls never work when I get datas from bundle, please help me.. I need comparison for bundle's data in 'if' block because I have to change textview according to data.

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 Answers4

2

use equals instead of == for compare strings as:

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

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

if (get.equals("C")) {
    txtV.setText("...");
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

You can use

if (get.equals("A")) { //my code ...
vbence
  • 20,084
  • 9
  • 69
  • 118
teamalpha5441
  • 741
  • 1
  • 6
  • 20
0

You cannot compare strings via ==, because that will only check for object identity, while two strings with the same content (e.g. A) might be separate objects. Use equals() instead:

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

Note the different order in the comparison. That prevents NullPointerExceptions if get should be null.

Here is a good explanation for this.

nfechner
  • 17,295
  • 7
  • 45
  • 64
  • If one guy already provided the exact answers then why you guyz are repeating the same answer again and again. Bad one – LuminiousAndroid Jul 17 '12 at 14:21
  • @Kabir121 Because I didn't provide the same answer. I provided more information and used a different approach which makes for better code. Also, the other answers were sent while I was already writing mine. – nfechner Jul 17 '12 at 15:05
-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("...");
    }
  }
}
AkashG
  • 7,868
  • 3
  • 28
  • 43
  • If one guy already provided the exact answers then why you guyz are repeating the same answer again and again. Bad one – LuminiousAndroid Jul 17 '12 at 14:21
  • @Kabir121 i think you are lacking in android..cant u see i have added if(result!=null) condition which you wont see in any code above.by not adding this might give null pointer exception if value is null.. – AkashG Jul 18 '12 at 04:42