0
2つのイメージ(item1.pngとitem2.png)が2つあり、スピナーのアイテムをクリックするとImageViewに各イメージを表示します。スピナーの "item1"が選択されたら、 "item1.png"イメージと "item2"イメージを表示したいと思います。クリックしたスピナーアイテムに基づいてイメージビューを変更しました
以下のコードでこれを実行しようとしていますが、機能していません。何が問題なのか分かりますか? MainActivityで
:
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);
int image1 = R.drawable.item1;
int image2 = R.drawable.item2;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//spinner.
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2) {
if (arg2 == 0) {
image.setImageResource(R.drawable.item1);
} else {
image.setImageResource(R.drawable.item2);
}
}
}
}
}
活動の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="8dp"
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.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/item1" />
<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>
あなたはときケース0画像invisiblityを設定する方法を知っていますか商品番号- 商品を選択
が選択されていますか?私はimage.setVisibility(View.INVISIBLE)を使用していますが、動作しません –
JonD