Friday, January 28, 2011

Simple Spinner w/ Array Adapter

Very basic stuff here, people. If you haven't yet, get acquainted with android spinner using the dev tutorial.


1) Calling the spinner, defining the array adapter.

       Spinner spinner= (Spinner) findViewById(R.id.spinner);
       ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
       this, android.R.layout.simple_spinner_item, items);
       spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinner.setPrompt("Select One");
       spinner.setAdapter(spinnerArrayAdapter );
spnRoute.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// your code here
}


@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

2) The string array


String[] items = { "lorem", "ipsum", "dolor", "sit", "amet"};

No comments:

Post a Comment