简体   繁体   中英

Show different images on different spinner selections

I am new to Android development and I am trying to create simple application including spinner ( 3 entries).

My goal is, to show a different image for each spinner that is selected .

With the help of some tutorials I managed to get the following code, but I have no idea, how to bind a image to a spinner entries.

activity_main.xml

<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=".MainActivity" >

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/Stockwerk"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="Plan der 8. Etage"
    />
</LinearLayout>
</RelativeLayout>

main_activity.java

package com.example.raumplan;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Spinner;

public class MainActivity extends Activity {

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

    addListenerOnSpinnerItemSelection();
}


public void addListenerOnSpinnerItemSelection() {
    spinner = (Spinner) findViewById(R.id.spinner1);
    spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

CustomOnItemSelectedListener.java

package com.example.raumplan;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), 
    parent.getItemAtPosition(pos).toString()+" ausgewählt",
    Toast.LENGTH_SHORT).show();


  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
  }

}

I'd really appreciate help from you guys :D Thanks

EDIT: Thanks for your help. I tried to add the following, but I do not know, what to replace "urImageView" with.

switch (pos) {
    case 0:
        urImageView.setImageResource(R.drawable.x);
        break;
    case 1:
        urImageView.setImageResource(R.drawable.y);         
        break;
    case 2:
        urImageView.setImageResource(R.drawable.z);         
        break;
    default:
        break;
}

In my activity_main.xml I have

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="Plan der 8. Etage"
    />
<ImageView
    android:id="@+id/imageView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="Plan der 7. Etage"
    />
<ImageView
    android:id="@+id/imageView3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="Plan der 6. Etage"
    />

You can simply add switch statement inside onItemSelected()

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
    parent.getItemAtPosition(pos).toString() + " ausgewählt", Toast.LENGTH_SHORT).show();

    switch (pos) {
        case 0:
           break;
        default:
           break;
    }
}

There are some steps for creating spinners with text and Image.

Steps :
  1.  Create Model (SpinnerModel.java) to store data for each spinner row.
  2.  Create a ArrayList to store Model (SpinnerModel.java) objects.
  3.  Store data in Models and Store Model objects in Arraylist.
  4.  Pass Model object Arraylist to custom adapter.
  5.  Custom Adapter use Arraylist data (Model Objects) and create rows for Spinner.
  6.  Create listener for Spinner and show spinner item selected values on activity.


This link given below will help you.

Custom Spinner With Image And Text

You can also use if else conditions to use show multiple image on selction. Something like this.

spinner = (Spinner) findViewById(R.id.spinner1);
imageview = (ImageView) findViewById(R.id.imageView1);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    //spinner.
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
                               int arg2, long arg3) {
        if(arg2==0)
        {
            imageview.setImageResource(R.drawable.apple);
        }
        else if(arg2==1)
        {
            imageview.setImageResource(R.drawable.microsoft);
        }
        else
        {
            imageview.setImageResource(R.drawable.google);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM