How to make a spinner populate another spinner?

旧时模样 提交于 2019-12-12 02:28:43

问题


How would you go about making a spinner populate another spinner based on the first spinners selection?

So for example:

Spinner1 items are vegetarian or meat eater.

  <string-array name="spinnerarray_veg_meat">
    <item >Vegetarian</item>
    <item >Meat eater</item> 
   </string-array> 

Spinner2 would then need to display either vegetarian meal names or meat eater ones depending on spinner1's selection.


回答1:


To do this you'll have to set a OnItemSelectedListener on your first Spinner to populate the second Spinner programmatically.

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

              if(position == 0) {
                   // Populate the Spinner2 with different values
              } else {
                   // Populate the Spinner2 with different values
              }

        }
        public void onNothingSelected(AdapterView<?> parent) {
            return;

        }
    });



回答2:


There are a number of ways to do it. One being, create an Array of meat items and one of vegetable items. In onItemSelected() of spinner1 set the adapter for spinner2 according to the position

Spinner Docs

This link has many useful functions and properties available to Spinners



来源:https://stackoverflow.com/questions/16751969/how-to-make-a-spinner-populate-another-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!