spinner android code example
Example 1: spinner android
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Example 2: how to set list data to android spinner
List<String> branchList = new ArrayList<>();
branchList.add("CHITTAGONG");
branchList.add("DHAKA");
ArrayAdapter<String> branchListAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, branchList);
branchListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spBranchName.setAdapter(branchListAdapter);
Example 3: how to access a spinner from another activity in android
First save the position of selected data from spinner in a String variable,
int positionOfSelectedDataFromSpinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
positionOfSelectedDataFromSpinner= position;
}
Then on button click send intent to Another activity with putExtra
Intent i = new Intent (this, activity2.class);
i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);
get int from getIntent in another activity
Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");
then set the position to spinner
spinner.setSelection(positionToShowToSpinner);
I think this my solve your problem.