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

在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]

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题