That's a java thing:
to compare Strings use something like if (strResult.equals("Four")),
as (strResult == "Four") checks if the operands are the same object
and not if they have the same content.
On 26 Aug., 19:49, erik wagner <[email protected]> wrote:
> Hi all, I have a very simple game that I'm trying to do for learning
> purposes. One of the options is selecting the number of players (2-4)
> via a spinner. When a choice is made from the spinner I am moving the
> value to a string and then using that string in a series of if/else if
> statements to (eventually) handle the option. I am running into the
> problem where the condition that (to me) seems like it should be true
> is evaluating to false.
> If I select "Two", the value moved to the variable is "Two" but the
> check for the variable == "Two" isn't getting hit.
> (hope that was all clear, backstory may not be needed but better safe
> than vague). Anyway, the spinner's OnSelectedItemListener...
>
> public class MyPlayerOnItemSelectedListener implements
> OnItemSelectedListener {
> public void onItemSelected(AdapterView<?> parent, View view, int
> pos, long id) {
> String strResult;
> strResult = parent.getItemAtPosition(pos).toString();
> if (strResult == "Two") {
> Toast.makeText(parent.getContext(), "this is the
> result when
> one is selected", Toast.LENGTH_LONG).show();
> } else if (strResult == "Three") {
> Toast.makeText(parent.getContext(), "three players /"
> +
> strResult + "/", Toast.LENGTH_LONG).show();
> } else if (strResult == "Four") {
> Toast.makeText(parent.getContext(), "four",
> Toast.LENGTH_LONG).show();
> } else {
> Toast.makeText(parent.getContext(), "something else
> /" +
> strResult + "/", Toast.LENGTH_LONG).show();
> }
>
> }
> public void onNothingSelected(AdapterView<?> parent) {
>
> }
> }
>
> No matter what I select using this code the Toast shows as "something
> else /" + what the value actually is + "/". i.e. if I chose 'Two' the
> toast reads "something else /Two/".
> Any help would be greatly appreciated on this. I'm certain it is
> something very fundamental that I'm just missing. Thanks in advance
> Erik Wagner
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en