JonD JonD - 23 days ago 7
Android Question

imageview is not changing correctly when spinner item is clicked

I have three images "item1, item2, item3" and I want to show the "item1.png" when the "Select items" option of the spinner is clicked and also at first (as default), "item2.png" when the item2 of the spinner is clicked and so on.

The issue is that is always appearing the image "item2.png" at first (as default), but at first I would like to show the "item1.png" that because it is the "Select items" item that is selected at first and I want to associate the "item1.png" to the "Select items" item.

Do you know why is the "item2.png" that is showing at first? Also if I click in the "Select items" the "item1.png" dont shows, it shows also the "item2.png". But when I click in the "item3" it shows corretly the "item3.png" image.

strings xml:

<string-array name="spinner_values">
<item>Select items</item>
<item>item1</item>
<item>item2</item>
</string-array>


code:

public class MainActivity extends AppCompatActivity {

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

Spinner spinner = (Spinner) findViewById(R.id.spinner2);
String string = String.valueOf(spinner.getSelectedItem());


final ImageView image = (ImageView)findViewById(R.id.image);



spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
image.setImageResource(R.drawable.item1);
case 1:
image.setImageResource(R.drawable.item2);
break;
case 2:
image.setImageResource(R.drawable.item3);
break;
default:
//Default image
//image.setImageResource(R.drawable.item2);
break;

}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

}
}


Activity xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ricardorei.tpc.MainActivity">

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="199dp"
android:layout_marginRight="58dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="58dp" />

<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="321dp"
android:layout_height="28dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text="Are Awesome!"
app:layout_constraintBottom_toTopOf="@+id/imageView2"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="295dp"
android:layout_height="28dp"
android:text="TextView"
app:layout_constraintBaseline_toBaselineOf="@+id/button3"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="32dp" />

<Spinner
android:id="@+id/spinner2"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="134dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:entries="@array/spinner_values"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>



</android.support.constraint.ConstraintLayout>

Answer Source
  1. You miss a break; in your switch statement on the end of case 0:
  2. Set your default selection with spinner.setSelection(0);