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 ?