Monday, November 24, 2014

android Spinner setAdapter ArrayAdapter

1.xml:

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />



2.code:



        List<String> _spinnerList;
ArrayAdapter _spinnerAdapter;

_spinnerList = new ArrayList<String>();
_spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, _spinnerList);


_spinnerList.add("1");
_spinnerList.add("2");
_spinnerList.add("3");

Spinner spinner = (Spinner)this.findViewById(R.id.spinner);
spinner.setAdapter(_spinnerAdapter);



3.event:



            _spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// get content:
// String content = (String) parent.getItemAtPosition(position);
// or:
// String item = _spinnerList.get(position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

});



4.last:



thanks!

No comments: