Spinner綁定鍵值對信息



1.創建信息類,用來存取鍵值對信息

package com.cclsol.gainmap.bean;

public class GetStation {
	private String stationId;
	private String stationName;
	
	public GetStation(){
		
	}
	
	public GetStation(String stationId, String stationName) {
		super();
		this.stationId = stationId;
		this.stationName = stationName;
	}

	public String getStationId() {
		return stationId;
	}

	public void setStationId(String stationId) {
		this.stationId = stationId;
	}

	public String getStationName() {
		return stationName;
	}

	public void setStationName(String stationName) {
		this.stationName = stationName;
	}

	
	@Override
	public String toString() {// 這是關鍵
		// 爲什麼要重寫toString()呢?因爲適配器在顯示數據的時候,如果傳入適配器的對象不是字符串的情況下,直接就使用對象.toString()
		// TODO Auto-generated method stub
		return stationName;
	}
}

2.activity類,綁定動態數據,並獲取選中的鍵值對信息

private void getStation() {
		WebService.callWebService(WebService.WEB_SERVER_URL,
				"getStationforNoXY", null, new WebServiceCallBack() {

					@Override
					public void callBack(SoapObject result) {
						// TODO Auto-generated method stub
						if (result != null) {
							SoapObject soregionObject = (SoapObject) result
									.getProperty("getStationforNoXYResult");
							Log.i("juan", soregionObject.toString());
							jiexiRegion(soregionObject);
						}
					}
				});
	}

	/**
	 * 解析數據,渲染到下拉框中
	 * 
	 * @param soapObject
	 */
	private void jiexiRegion(SoapObject soapObject) {
		List<GetStation> stationlist = new ArrayList<GetStation>();

		for (int i = 0; i < soapObject.getPropertyCount(); i += 2) {

			stationlist.add(new GetStation(
					soapObject.getProperty(i).toString(), soapObject
							.getProperty(i + 1).toString()));
		}

		Log.i("juan", "stationlist" + stationlist.toString());
		ArrayAdapter<GetStation> station = new ArrayAdapter<GetStation>(this,
				android.R.layout.simple_spinner_item, stationlist);
		station.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		StationSpinner.setAdapter(station);
		StationSpinner.setSelection(0, true);
		StationSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				
				Toast.makeText(
						UploadDialog.this,
						"鍵:"
								+ StationSpinner.getSelectedItem().toString()
								+ "、"
								+ ((GetStation) StationSpinner
										.getSelectedItem()).getStationId()
								+ ",值:"
								+ ((GetStation) StationSpinner
										.getSelectedItem()).getStationName(),
						Toast.LENGTH_LONG).show();
				myStationID = ((GetStation) StationSpinner.getSelectedItem())
						.getStationId();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(UploadDialog.this, "未選擇", Toast.LENGTH_SHORT)
						.show();
			}
		});

	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章