Android fragment spinner 修改默认显示的文本内容

        我不得不说spinner是一个坑,很多东西和api都挺不好用的,只是在界面的展示上面给了我们一个便利,其余关于触发、默认显示的文本、更改默认显示的文本都有很多坑。我的需求是这样的,首次进入这个界面,spinner当然是显示默认的第一条数据,当我点击了修改的界面,这个显示的日期需要传递到spinner进行显示,也就是要修改默认显示的spinner文本,关键在于如下代码:

Spinner.setAdapter(expireTimeAdapter);
					Adapter.notifyDataSetChanged(); // 通知spinner刷新数据
					Spinner.setSelection(0, true);

      也就是重新设置适配器,然后通知spinner更新列表数据,然后将第几行选中,spinner.setSelecttion(0,true)这个方法如果改成spinner.setSelection(0)是不会触发spinner的列表点击事件的,需要后面加一个参数true,意思是立即执行这个下拉列表的点击事件。感谢如下两篇博文:

androd spinner 更新默认文本

spinner.setselecttion方法的含义  

 我的主要代码如下:

package ****;

import java.text.ParseException;
import java.text.SimpleDateFormat;


public class PriceWarning2ModifyFragment extends ControlFragment implements
		StationEventListener {




	private void setEffectDataForSpinner(View view) {


		// **********第2个下拉列表,有效日期************
		expireTimeSpinner = (Spinner) view.findViewById(R.id.effectivedate);
		// 将可选内容与ArrayAdapter连接起来
		expireTimeAdapter = new ArrayAdapter(getActivity(),
				R.layout.activity_tipsprice_spinner, m);

		// //设置下拉列表的风格
		expireTimeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// 将adapter 添加到spinner中
		expireTimeSpinner.setAdapter(expireTimeAdapter);

		// 添加事件Spinner事件监听
		expireTimeSpinner
				.setOnItemSelectedListener(new effectivedateSpinnerSelectedListener());
		expireTimeSpinner.setSelection(0, true);

		// 设置默认值
		expireTimeSpinner.setVisibility(View.VISIBLE);

	}

	// 使用数组形式操作
	class effectivedateSpinnerSelectedListener implements
			OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView arg0, View arg1, int arg2,
				long arg3) {
			switch (arg2) {
			case 0:
				int flagmodify = DocCaptain.getInstance().getPricemodify();


				if (flagmodify == 0) {
				
					if (modipriceType == CommDataInterface.ORDER_EXPIRE_TYPE_USER_DEFINED) {
						((TextView) arg1).setText(timeAddPrice);
					} else if (modipriceType == CommDataInterface.ORDER_EXPIRE_TYPE_DAY) {
						((TextView) arg1).setText("N");
					} else if (modipriceType == CommDataInterface.ORDER_EXPIRE_TYPE_WEEK) {
						((TextView) arg1).setText("");
					} else {
					
						((TextView) arg1).setText("效");
					}
					break;
				} else {
					DocCaptain.getInstance().setPricemodify(1);
					effectivetype = CommDataInterface.ORDER_EXPIRE_TYPE_USER_DEFINED;
					effectivetime = chooseDate(1);
					String effectivetimeOne = TimeUtil
							.formatTime(effectivetime);
					((TextView) arg1).setText(effectivetimeOne);

					break;
				}

			case 1:
				effectivetype = CommDataInterface.ORDER_EXPIRE_TYPE_USER_DEFINED;
				effectivetime = chooseDate(2);
				String effectivetimeTwo = TimeUtil.formatTime(effectivetime);
				((TextView) arg1).setText(effectivetimeTwo);
				break;


			default:
				break;
			}

		}

		private Date chooseDate(int hours) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar ca = Calendar.getInstance();
			ca.setTime(new Date());
			ca.add(Calendar.HOUR_OF_DAY, hours);
			return ca.getTime();
		}

		@Override
		public void onNothingSelected(AdapterView arg0) {
		}
	}





	// 当界面隐藏之后重新show,监听
	@Override
	public void onHiddenChanged(boolean hidd) {
		if (!hidd) {

			if (getActivity() != null) {
				// 从界面传递参数过来
				modipriceType = DocCaptain.getInstance().getModieffType();
				modiamount = DocCaptain.getInstance().getModiamount();
				instrument = DocCaptain.getInstance()
						.getModifyTipspriceInstrument();
				guid = DocCaptain.getInstance().getGuid();
				timeAddPrice = DocCaptain.getInstance().getModieffTime();

				if (modipriceType == 0) {
					expireTimeSpinner.setAdapter(expireTimeAdapter);
					expireTimeAdapter.notifyDataSetChanged(); // 通知spinner刷新数据
					expireTimeSpinner.setSelection(0, true);
				} else if (modipriceType == 1) {
					expireTimeSpinner.setSelection(6);
				} else if (modipriceType == 2) {
					expireTimeSpinner.setSelection(7);
				} else {
					expireTimeSpinner.setAdapter(expireTimeAdapter);
					expireTimeAdapter.notifyDataSetChanged(); // 通知spinner刷新数据
					expireTimeSpinner.setSelection(0, true);
				}

				loadPriceWaringData();
				refreshPriceWarningUI();
			}
		}

	}

}
有问题可以沟通。

你可能感兴趣的:(Android)