-1

i was trying to work out on my spinner tutorial but it doesnt seem to show the header. can anyone guide me on which part that i might have overlook?

public class HelloSpinnerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        spinnertest = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.Planets, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnertest.setAdapter(adapter);
        spinnertest.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }
    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The planet is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}

main.xml

<Spinner 
    android:id="@+id/Spinner01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:prompt = "@string/planet_prompt"
    android:entries ="@array/Planets">
</Spinner>

strings.xml

<string name="app_name">Spinner</string>
 <string name="planet_prompt"> Select a planet </string>
<string-array name="Planets">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
    <item>Pluto</item>
</string-array>
MByD
  • 135,866
  • 28
  • 264
  • 277
user1069006
  • 19
  • 1
  • 3

2 Answers2

0

Not quite sure if this is possible solution will help (you may have a particular reason to try it your way), but in the past I've just added the Prompt text to the top of the Spinner string-array (at position 0), and then it shows by default in the widget. Then, in 'onItemSelected', I ignore anything if pos == 0.

iaindownie
  • 1,046
  • 12
  • 28
0

I don't know exactly what you mean by "header", but the tutorial displays a prompt in a TextView in main.xml that you do not show:

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="@string/planet_prompt" />

Also, since you are initializing your spinner in the XML, you don't need to do it in code. Try this:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Sparky
  • 8,437
  • 1
  • 29
  • 41