简体   繁体   中英

How to display ImageView when I'm choose spinner item

 private String[] genre = { "Action", "Adventure", "Comedy", "Biography", "Drama", "Family", "Horror", "Romance" }; 
 private Integer images[] = { R.drawable.img_action, R.drawable.img_adventure, R.drawable.img_biografy, R.drawable.img_comedy, R.drawable.img_drama, R.drawable.img_family, R.drawable.img_horror, R.drawable.img_romantis };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setInitialImage();

    spinner = (Spinner) findViewById(R.id.label_spinner);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(       // make array adapter
            this, android.R.layout.simple_spinner_item, genre
        );

    spinner.setAdapter(adapter);   // connect adapter with  spinner
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {     // get item from spinner

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            int index = spinner.getSelectedItemPosition();

            if () {

                currImage = images[0];  
                Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
                .show();
            }
            setCurrentImage(); // called method

        }

    });     

}

how, when I click one of the spinner items, then the image of the movie genre will appear. what should I type in the if statement?

anyone can help me solve this problem?

Modify your onItemSelectedMethod to below

@Override
public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
      currImage = images[position];  
      Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
                .show();
      setCurrentImage(); // called method
}

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