简体   繁体   中英

Different content for each Spinner item in the same Activity

How can I show different content after selecting an item from the Spinner ? I want to create a Spinner with locations of chain stores.

I want the spinner to be always there on top. The only thing that changes to be the content under the spinner

Create a simple method in your activity to refresh the layout below the Spinner (which will remain untouched). That method will be called from a OnItemSelectedListener set on the Spinner . It would be something like this:

private void changeAdress(int newSelectedAdress) {
    // The ImageView and the TextView will be already in the layout
    ImageView map = (ImageView) findViewById(R.id.theIdOfTheImage);
    // Set the image. You know the current address selected by the user
    // (the newSelectedAddress int) so get it from the array/list/database
    // where you stored it
    // also set the image
}

Called the method above from the onItemSelected callback:

yourSpinnerRefference.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
                          changeAddress(position);              
        }

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

        }
    });

If this is not what you want, please explain.

Edit: Make two arrays to hold your data:

int[] images = {R.drawable.imag1, R.drawable.imag2 ..etc...};
//also for the text
String[] text = {"text1", "text2 ...etc...};

Then use those two arrays in the method I recommended above:

private void changeAdress(int newSelectedAddress) {
     ((ImageView)findViewById(R.id.mapView1)).setImageResource(images[newSelectedAddress]); 
     // assing an id to the TextView in your layout and do the same as above.
}

There is no need for multiple ImageView and TextViews .

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