从android中的web服务数据填充微调器
我必须用web服务数据(XML解析)填充spinner。它包含两个属性,如“ID”和“Name”。 代码如下:从android中的web服务数据填充微调器,android,web-services,spinner,Android,Web Services,Spinner,我必须用web服务数据(XML解析)填充spinner。它包含两个属性,如“ID”和“Name”。 代码如下: try { ArrayList<String> listdata = new ArrayList<String>(); URL url = new URL( "http://10.0.2.2/WebService/Service.asmx/GetService")
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();
}
试试看{
ArrayList listdata=新的ArrayList();
URL=新URL(
"http://10.0.2.2/WebService/Service.asmx/GetService");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“表”);
for(int i=0;i适配器=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,listdata);
adapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spinMobileProvider.setAdapter(适配器);
}
捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
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())));
private void发送(视图v,字符串Url){
HttpPost postMethod=新的HttpPost(Url);
List regParams=new ArrayList();
DefaultHttpClient hc=新的DefaultHttpClient();
添加(新的BasicNameValuePair(“\fname”,edtFname.getText().toString());
添加(新的BasicNameValuePair(“\u lname”,edtLname.getText().toString());
添加(新的BasicNameValuePair(“_email”,edtEmail.getText().toString());
添加(新的BasicNameValuePair(“_password”,edtPassword.getText().toString());
添加(新的BasicNameValuePair(“_地址”,edtAddress.getText().toString());
add(新的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>
11
细胞南部
10
板球
4.
斯普林特耐克斯特尔酒店
和输出类似
我必须在微调器列表中显示名称,并将特定的id传递给数据库。如何将id传递到文本的后端?将id和微调器值作为一组对象放入数组列表中
Class spinnerObj {
String id;
String spinnervalue;
}
Arraylist<spinnerObj> sp = new Arraylist<spinnerObj>():
Class spinnerObj{
字符串id;
字符串值;
}
Arraylist sp=新建Arraylist():
现在,使用此arraylist sppinnervalue中的值填充微调器
在微调器的onValueChange方法中,您将获得位置,该位置将与您添加的顺序相同,您可以做的是调用sp.get(position).getID()您将获得与微调器值关联的id。首先从webservice抓取数据,并使用两列(如id和值)将其保存到数据库中…以列表形式单独从数据库中获取值数据并与微调器绑定