Karthik Karthik - 3 months ago 13
Android Question

How to set selecteditemposition after swapping the spinner in android

My app consists of two spinners and one button, i have selected each value from each spinner, later i had swapped the spinner adapters using button click, when spinner adapters are modified, the result by default represents initial values, no matter what ever values i have given.

This is how my MainActivity looks like

public class MainActivity extends AppCompatActivity {

private Spinner alphabets, numbers;
private Button swapSpinners;
private ArrayAdapter<CharSequence> alphabetsAdapter, numbersAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

alphabets = (Spinner) findViewById(R.id.alphabets);
numbers = (Spinner) findViewById(R.id.numbers);
swapSpinners = (Button) findViewById(R.id.swapSpinner);


alphabetsAdapter = ArrayAdapter.createFromResource(this,
R.array.alphabets, android.R.layout.simple_spinner_item);
alphabetsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
alphabets.setAdapter(alphabetsAdapter);

numbersAdapter = ArrayAdapter.createFromResource(this,
R.array.numbers, android.R.layout.simple_spinner_item);
numbersAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numbers.setAdapter(numbersAdapter);

swapSpinners.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (alphabets.getAdapter().equals(alphabetsAdapter)) {
alphabets.setAdapter(numbersAdapter);
numbers.setAdapter(alphabetsAdapter);
} else {
alphabets.setAdapter(alphabetsAdapter);
numbers.setAdapter(numbersAdapter);
}
}
});}
}


This is how my activity_main looks like

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="karthik.spinnerswap.MainActivity">

<Spinner
android:id="@+id/alphabets"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/swapSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/alphabets"
android:text="Swap Spinners" />
<Spinner
android:id="@+id/numbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/swapSpinner" />
</RelativeLayout>


This is how my strings.xml looks like

<resources>
<string name="app_name">SpinnerSwap</string>

<string-array name="alphabets">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
<item>G</item>
<item>H</item>
<item>I</item>
<item>J</item>
</string-array>

<string-array name="numbers">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
</string-array>




Selecting values, before swapping the spinners
before swapping

spinners after swapping
after swapping

I need to show the selected values after swapping the spinners

Answer

Use this it solve your problem ......

swapSpinners.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          int positionSpinner1 = alphabets.getSelectedItemPosition() ;
          int positionSpinner2 = numbers.getSelectedItemPosition() ;
          if (alphabets.getAdapter().equals(alphabetsAdapter)) {
            alphabets.setAdapter(numbersAdapter);
            numbers.setAdapter(alphabetsAdapter);
            } else {
            alphabets.setAdapter(alphabetsAdapter);
            numbers.setAdapter(numbersAdapter);
            }
          alphabets.setSelection(positionSpinner2);
          numbers.setSelection(positionSpinner1);
        }
    });

Note:-Use int position to interchange their values.....