Spinner值的获取和设置默认值

时间:2021-02-08 09:55:18
	//设置Spinner样式android.R.layout.simple_spinner_item,绑定数组数据array,将数组内容和ArrayAdapter连接
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, array);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner.setAdapter(adapter);
		//设置默认选中
		mSpinner.setSelection(2,true);
		//添加监听事件
		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long id) {
				//获取选中值
				Spinner spinner = (Spinner) adapterView;
				String data = (String) spinner.getItemAtPosition(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

  参考:http://www.cnblogs.com/over140/archive/2010/09/25/1834469.html

二. Spinner的数据绑定方式

1、 通过数组字符串绑定

   

private static final String[] m_Countries = { "111", "222", "333", "444", "555" };   //定义数组

   public String[]  petArray = new String[] { "狗", "猫", "其他" };

    使用方法如上例

2、通过资源文件配置:比如values文件夹中的:arrays.xml、colors.xml、ids.xml、strings.xml、styles.xml等等

<string-array name="debitCardNumber">
        <item>信用卡111</item>
        <item>信用卡222</item>
        <item>信用卡333</item>
        <item>信用卡444</item>
        <item>信用卡555</item>
    </string-array>

  绑定方法:

 // 准备一个数组适配器  
        // R.array.colors : 直接从strings.xml去数据  
        // android.R.layout.simple_spinner_item : 设置Spinner样式(无下拉列表时)  
        ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter  
                .createFromResource(this, R.array.colors,  
                        android.R.layout.simple_spinner_item);  
        // 设置下拉列表样式  
        arrayAdapter  
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        // 为下拉列表设置适配器  
        spinner.setAdapter(arrayAdapter);  
        // 为下拉列表绑定事件监听器  
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  
            @Override  
            public void onItemSelected(AdapterView<?> parent, View view,  
                    int position, long id) {  
                Toast.makeText(  
                        SpinnerDemoActivity.this,  
                        "选择的色彩: "  
                                + parent.getItemAtPosition(position).toString(),  
                        Toast.LENGTH_LONG).show();  
            }  
  
            @Override  
            public void onNothingSelected(AdapterView<?> parent) {  
  
            }  
        });  

  3、数据库存储

三、 Spinner实现省市二级联动

ArrayAdapter<String> adapterProvince = new ArrayAdapter<String>(
				this,
				android.R.layout.simple_spinner_item, provinceArray);
		adapterProvince
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinnerProvince.setAdapter(adapterProvince);
//		mSpinnerProvince.setSelection(2,true);//设置默认选中
		mSpinnerProvince
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> adapterView,
							View view, int position, long id) {
						Spinner spinner = (Spinner) adapterView;
						String pro = (String) spinner
								.getItemAtPosition(position);
						mUserProvince = pro;

						ArrayAdapter<String> adapter = new ArrayAdapter<String>(
								this,
								android.R.layout.simple_spinner_item, city);
						adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
						mSpinnerCity.setAdapter(adapter);
						mSpinnerCity
								.setOnItemSelectedListener(new OnItemSelectedListener() {

									@Override
									public void onItemSelected(
											AdapterView<?> adapterView,
											View view, int position, long id) {
										Spinner spinner = (Spinner) adapterView;
										String pro = (String) spinner
												.getItemAtPosition(position);
										mUserCity = pro;
										Log.v("city", "mUserProvince:"
												+ mUserProvince
												+ "   mUserCity:" + mUserCity);
									}

									@Override
									public void onNothingSelected(
											AdapterView<?> arg0) {
										// TODO Auto-generated method stub

									}

								});
					}

  直接用string设置默认选中的方法:

int pos=adapter.getPosition(str);

mSpinnerCity.setSelection(pos);