0

I get a string form a list and try to compare it with some strings in the values and then do some stuff

for(int i=0; i<sizeOfList; i++){

    String LIST_TITLE;

    LIST_TITLE = list_title.get(i); //the List list_title includes some strings

    if(LIST_TITLE.equals(R.string.percentbattery)) {
        //do stuff
        Log.d("EQUAL!","" + LIST_TITLE);
    } else if(LIST_TITLE.equals(R.string.screenrecorder) == true) {
        //do stuff
        Log.d("EQUAL!","" + LIST_TITLE);
    } else if(LIST_TITLE.equals(R.string.eightsms) == true) {
        //do stuff
        Log.d("EQUAL!","" + LIST_TITLE);
    } else {
        // do stuff
        Log.e("TITLE NOT EQUAL","" + LIST_TITLE);
    }
}

If I compare my LIST_TITLE with the (R.string. ...) in my Logcat they are equal, but I get only the "TITLE NOT EQUAL" Log from the else statement.

Is there another way to compare these strings? the "==" method also don't work.

Kroenig
  • 644
  • 7
  • 16
  • `if (condition == true)` is considered as bad style. It is easy to make mistake where you use `=` instead of `==` which means you are assigning `true` to `condition` (if it is variable). That is why it is better to just use `if (condition)` or if you really must compare it with `true` then use yoda style `if (true == condition)` - here even if you use `=` instead of `==` you will get compilation error because we can assign value to variables, not to other values. – Pshemo Sep 11 '14 at 15:38

4 Answers4

3

R.string.percentbattery is not a String, it's an Integer that is the ID to reference the string.

what u want is:

LIST_TITLE.equals(context.getResources.getString(R.string.percentbattery))
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Even if I use your solution or `LIST_TITLE.equals("String")` it doesn't work. More details: I use this in an `AsyncTask (doInBackground)`. I fetch titles from a website and save them in a `List list_title`. Now I try to compare this list_title with some strings to set an icon. In this AsyncTask I have no context, so I used your solution without context. `LIST_TITLE.equals(getResources().getString(R.string.percentbattery))` Could this be the mistake? Thanks – Kroenig Sep 11 '14 at 15:52
  • How exactly you call it makes no different. If inside an `Activity` you can call `getString(int)` without anything else. That really where this code is. The important to fix your code is to understand that `R.string.anything` is NOT a String, so you can't compare it with one, you need to get the actual string to be able to compare it. – Budius Sep 11 '14 at 15:56
2
LIST_TITLE.equals(R.string.percentbattery)

This is incorrect, because you're trying to compare string with resource ID You should get the string from resource first:

LIST_TITLE.equals(getResources().getString(R.string.percentbattery))
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
0

R.string.xxx is an int. You need to get the String from that res

Something like

if(LIST_TITLE.equals(getResources().getString(R.string.percentbattery)))

This is assuming you have Activity Context available. Otherwise, you would need to add a Context variable in front of getResources()

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

R.string.some_id is just an integer by which you can get the String from the resources. So in order to compare Strings correctly in you case you have to do:

String precentBattery = getResources().getString(R.string.percentbattery);
if (LIST_TITLE.equals (percentBattery)) ...
cliffroot
  • 1,839
  • 17
  • 27