1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package com.example.shayw.spinnerapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
String[] items = new String[]{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5",
"Item6",
"Item7",
"Item8",
"Item9",
"Item10",
"Item11",
"Item12",
"Item13",
"Item14",
"Item15",
"Item16",
"Item17",
"Item18",
"Item19",
"Item20",
"Item21",
"Item22",
"Item23",
"Item24",
"Item25",
"Item26",
"Item27",
"Item28",
"Item29",
"Item30",
"Item31",
};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.support_simple_spinner_dropdown_item,items
);
// Specify the layout to use when the list of choices appears
spinnerArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
staticSpinner.setAdapter(spinnerArrayAdapter);
// Apply the adapter to the spinner
staticSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
} |
Partager