Skip to content Skip to sidebar Skip to footer

Compare Two Strings With .equals() Don't Work

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

Solution 1:

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))

Solution 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))

Solution 3:

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()

Solution 4:

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)) ...

Post a Comment for "Compare Two Strings With .equals() Don't Work"