a124055852 2011-05-19 11:51
浏览 537
已采纳

在android开发中,如何实现两个spinner 关联

在android开发中,如何实现两个spinner 关联,即点击第一个spinner后可以在第二个spinner中动态添加数据?

  • 写回答

1条回答 默认 最新

  • iteye_8576 2011-05-19 12:39
    关注

    main.xml

    [code="xml"]<?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="90px"
    android:layout_height="42px"
    android:layout_marginTop="3px"
    android:layout_marginLeft="60px"/>

    <Spinner android:id="@+id/list2"
        android:layout_width="90px" 
        android:layout_height="42px"
        android:layout_marginTop="3px"
        android:layout_marginLeft="60px"/>      
    


    [/code]
    Activity

    [code="java"]public class AndroidTestActivity extends Activity {
    private Spinner spinner1;
    private Context context;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = this;
    spinner1 = (Spinner) findViewById(R.id.list1);
    String[] m={"aaa","bbb"};
    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,m);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(spinnerSelectedListener);
    }

    private Spinner.OnItemSelectedListener spinnerSelectedListener = new Spinner.OnItemSelectedListener()  
    {  
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
                long arg3) {  
            String selectedValue = (String)spinner1.getSelectedItem();
            Spinner spinner2 = (Spinner) findViewById(R.id.list2);
            String[] m={selectedValue};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                    android.R.layout.simple_spinner_item, m);
            spinner2.setAdapter(adapter);
        }  
    
        public void onNothingSelected(AdapterView<?> arg0) {  
            // TODO Auto-generated method stub  
        }         
    };
    

    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 目前wxid加好友的渠道都被和谐了吗
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出