我将使用Web服务数据(XML解析)填充微调器。它包含两个属性,如“ID”和“名称”。 代码如下:
try {
ArrayList<String> listdata = new ArrayList<String>();
URL url = new URL(
"http://10.0.2.2/WebService/Service.asmx/GetService");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Table");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element providerID = (Element) node;
NodeList providerIDlist = providerID.getElementsByTagName("id");
Element providerIDelement = (Element) providerIDlist.item(0);
providerIDlist = providerIDelement.getChildNodes();
MProviderIDL = providerIDlist.item(0).getNodeValue();
//mprovider.put("MproviderID", MProviderIDL.toString());
listdata.add(MProviderIDL);
Element providerName = (Element) node;
NodeList providerNamelist = providerName.getElementsByTagName("value");
Element prividerNameElement = (Element) providerNamelist.item(0);
providerNamelist = prividerNameElement.getChildNodes();
//AddImg[i].setTag(((Node) AdvImglist.item(0)).getNodeValue());
MProviderNameL = providerNamelist.item(0).getNodeValue();
//mprovider.put("MproviderName", MProviderNameL.toString());
listdata.add(MProviderNameL);
}
Spinner spinMProvider=(Spinner)findViewById(R.id.spinMProvider);
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1,listdata);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spinMobileProvider.setAdapter(adapter);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
将值发送到数据库..
private void Send(View v,String Url) {
HttpPost postMethod=new HttpPost(Url);
List<NameValuePair> regParams=new ArrayList<NameValuePair>();
DefaultHttpClient hc=new DefaultHttpClient();
regParams.add(new BasicNameValuePair("_fname",edtFname.getText().toString()));
regParams.add(new BasicNameValuePair("_lname",edtLname.getText().toString()));
regParams.add(new BasicNameValuePair("_email",edtEmail.getText().toString()));
regParams.add(new BasicNameValuePair("_password",edtPassword.getText().toString()));
regParams.add(new BasicNameValuePair("_address",edtAddress.getText().toString()));
regParams.add(new BasicNameValuePair("Provider", Integer.toString(spinMProvider.getSelectedItemPosition())));
我的Xml数据......
<Table diffgr:id="Table4" msdata:rowOrder="3">
<id>11</id>
<value>Cellular South</value>
</Table>
<Table diffgr:id="Table5" msdata:rowOrder="4">
<id>10</id>
<value>Cricket</value>
</Table>
<Table diffgr:id="Table6" msdata:rowOrder="5">
<id>4</id>
<value>Sprint NexTel</value>
</Table>
输出如this .....
我要在微调器列表中显示名称并将特定的id传递给数据库。如何获取ID到文本的后端?
答案 0 :(得分:0)
将id和spinner值放在数组列表中作为一组对象,比如说
Class spinnerObj {
String id;
String spinnervalue;
}
Arraylist<spinnerObj> sp = new Arraylist<spinnerObj>():
现在使用此arraylist sppinnervalue;
中的值填充您的微调器在微调器的onValueChange方法中,你将获得的位置与你添加的顺序相同,你可以做的是调用sp.get(position).getID()你将得到你的id与微调值相关联。