1

Why is this not working ?!

if(itemx == "Test number item 0")
    {
    Log.i("Dropdown", "inside if");
    us_lo_ans_hold.setText("0x");
    }; 

if itemx is a String and it has the string 'Test number item 0'.

I am creating my first spinner.

Here is the spinner code within the onCreate block of the Activity:

    Spinner us_lo_spinner = (Spinner) findViewById(R.id.ul_ans_spinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.test_defaults , android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    us_lo_spinner.setAdapter(adapter); 

 // Spinner click listener 
us_lo_spinner.setOnItemSelectedListener(this);

Here is the String array in the strings.xml:

<string name="spin_title_ulover_ans">Spinner default:</string> 
<string-array name="test_defaults"> 
    <item>Test number item 0</item>
    <item>Test number item 1</item> 
    <item>Test number item 2</item> 
    <item>Test number item 3</item> 
    <item>Test number item 4</item> 
</string-array>

Here is the onItemSelected:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{
    Log.i("XXX_Dropdown", "XXXX");
    TextView us_lo_ans_hold = (TextView)findViewById(R.id.us_lo_ans_holdtest);
    // On selecting a spinner item 
    String itemx = arg0.getSelectedItem().toString();
    us_lo_ans_hold.setText("oppp12");
    Log.i("Dropdown", "item: " + itemx);
    Log.i("Dropdown", "arg1: " + arg1);
    Log.i("Dropdown", "arg2: " + arg2);
    Log.i("Dropdown", "arg3: " + arg3);
    // Showing selected spinner item 
    if(itemx == "Test number item 0")
        {
        Log.i("Dropdown", "inside if");
        us_lo_ans_hold.setText("0x");
        }; 

}

From all the Log.i(,)s I put in I can tell the itemx is correct, it has the correct string when an item in the spinner is selected. Yet the IF will not work.

If I change the IF to this:

if(arg2 == 0)
    {
    Log.i("Dropdown", "inside if");
    us_lo_ans_hold.setText("0x");
    }; 

it will work; BUT that is not what I want!

So... why is this not working ?!:

if(itemx == "Test number item 0")
    {
    Log.i("Dropdown", "inside if");
    us_lo_ans_hold.setText("0x");
    }; 

if itemx is a String and it has the string 'Test number item 0'. ?


also how or when would 'onNothingSelected' be called? When you hit the back button ? When you select the item already selected? My 'onNothingSelected' is never called !

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    Log.i("Dropdown", "in NOTHING SELECTED");
    TextView us_lo_ans_hold = (TextView)findViewById(R.id.us_lo_ans_holdtest);
    us_lo_ans_hold.setText("nothing selected");
}

Tried putting in the onCreate block of the Activity like the:

    // Spinner click listener 
    us_lo_spinner.setOnItemSelectedListener(this);

us_lo_spinner.onNothingSelected(this)   

but the ~compiler~ does like that.

So... how or when is 'onNothingSelected' called and how is it hooked up to a spinner ?

  • U should use itemx.equals("Test number item 0") instead of == operator ... – Hardik4560 Oct 21 '12 at 18:42
  • Since I started answering questions... it is ridiculous how much questions has been asked on string comparison in Java. – Lews Therin Oct 21 '12 at 18:42
  • Read up on your basic Java knowledge: http://alvinalexander.com/java/edu/qanda/pjqa00001.shtml - why == doesn't work on String. – Darwind Oct 21 '12 at 18:43

2 Answers2

4

If your itemx is a variable then you cant compare two strings with ==. Use instead items.equals("Test number item 0");

== compares objects and equals() compare string values. Take a look a this post.

Community
  • 1
  • 1
yugidroid
  • 6,640
  • 2
  • 30
  • 45
2

Never compare Strings with ==, use equals:

if(itemx.equals("Test item 0")){...

As for onNothingSelected, the documentation states it will be called for example if you delete the selected item from your adapter.

molnarm
  • 9,856
  • 2
  • 42
  • 60