
The Spinner object brings only the GUI-functionality, but the SpinnerAdapter provides the common functionality. It’s a bridge between the Spinner and strings with whom this widget works.
The adapter of spinner offers two different ways of interacting with the data. The first method is to use spinner, is using the Left-Right-keys to switch between strings. The second method uses a drop down list for choosing the required string that appears when you click on right edge of the spinner ("<>").

ArrayAdapter is used for filling data. We will use a List to store the strings we want to display. An code-example using the widget (see below).
main.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffc5d1d4"
- >
- <TextView
- id="@+id/lblAllCountries"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="List of countries"
- android:textSize="13dip"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="20dip"
- />
- <Spinner
- id="@+id/spnCountries"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_below="@+id/lblAllCountries"
- />
- <TextView
- id="@+id/lblNewCountry"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="13dip"
- android:text="Input new country"
- android:layout_below="@+id/spnCountries"
- android:layout_centerHorizontal="true"
- />
- <EditText
- id="@+id/txtNewCountry"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:layout_below="@+id/lblNewCountry"
- android:layout_centerHorizontal="true"
- android:textSize="13dip"
- />
- <Button
- id="@+id/btnAddItem"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:text="Add country"
- android:gravity="center"
- android:layout_centerInParent="true"
- android:layout_below="@+id/txtNewCountry"
- android:textSize="13dip"
- />
- <Button
- id="@+id/btnRemoveItem"
- android:layout_width="150dip"
- android:layout_height="wrap_content"
- android:text="Remove current country"
- android:layout_below="@+id/btnAddItem"
- android:gravity="center"
- android:layout_centerHorizontal="true"
- android:textSize="13dip"
- />
- </RelativeLayout>
Parsed in 0.006 seconds, using GeSHi 1.0.8.4
SpinnerSample.java
Using java Syntax Highlighting
- package maximyudin.spinner;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Spinner;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.view.View;
- import android.widget.EditText;
- public class SpinnerSample extends Activity {
- private static final String[] mCountries = {
- “Russia“, “Germany“, “Ukraine“, “Belarus“, “USA“
- };
- private List<String> allcountries;
- private ArrayAdapter<String> aspnCountries;
- private EditText txtNewCountry;
- private Spinner spnCountries;
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- txtNewCountry = (EditText) findViewById(R.id.txtNewCountry);
- spnCountries = (Spinner) findViewById(R.id.spnCountries);
- allcountries = new ArrayList<String>();
- for (int i = 0; i < mCountries.length; i++) {
- allcountries.add(mCountries[i]);
- }
- aspnCountries = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, allcountries);
- aspnCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spnCountries.setAdapter(aspnCountries);
- final Button btnAddItem = (Button) findViewById(R.id.btnAddItem);
- btnAddItem.setOnClickListener(btnAddItemListener);
- final Button btnRemoveItem = (Button) findViewById(R.id.btnRemoveItem);
- btnRemoveItem.setOnClickListener(btnRemoveItemListener);
- }
- private Button.OnClickListener btnAddItemListener =
- new Button.OnClickListener() {
- public void onClick(View v) {
- allcountries.add(txtNewCountry.getText().toString());
- aspnCountries = new ArrayAdapter<String>(SpinnerSample.this,
- android.R.layout.simple_spinner_item, allcountries);
- spnCountries.setAdapter(aspnCountries);
- }
- };
- private Button.OnClickListener btnRemoveItemListener =
- new Button.OnClickListener() {
- public void onClick(View v) {
- allcountries.remove(spnCountries.getSelectedItemIndex());
- aspnCountries = new ArrayAdapter<String>(SpinnerSample.this,
- android.R.layout.simple_spinner_item, allcountries);
- spnCountries.setAdapter(aspnCountries);
- }
- };
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


P.S. Here's http://www.maximyudin.com/?p=17 my article is on english and russian languages



I tried almost all known ways for me for solving this problem (new adapter every time), but without result
If you're not against, I will post your code in my blog, ok?

