我开发了一个Android应用程序。
我已经传递了xml标签。
这里我遇到了一些问题。请给我一个解决方案。
这是我的xml Feed:
<root>
<Categories>
<Category title="books" categoryid="1" image="http://google.com/dsdsds.png" >
<SubCategory>
<Subcategory name="Android" subcategoryid="2" subimage="http://google.comdsdsdsd.jpg" >
<Products>
<product name="android programming" productid="3" >
<Retailers>
<Retailer name='sfsdsds' />
<Retailer name='sdsdsdsd' />
</Retailers>
在这里,我已将零售商传递给以下代码:
if (productNode
.hasChildNodes()) {
NodeList retailerNameNL = productElmt
.getElementsByTagName("Retailers");
if (retailerNameNL.getLength() > 0) {
for (int rnCnt = 0; rnCnt < retailerNameNL
.getLength(); rnCnt++) {
Node retailerNameNode = retailerNameNL
.item(rnCnt);
if (retailerNameNode.hasChildNodes()) {
Element retailernamesElmt = (Element) retailerNameNode;
NodeList retailernamesChildNL = retailernamesElmt
.getElementsByTagName("Retailer");
if (retailernamesChildNL.getLength() > 0) {
RetailerNameAL = new ArrayList<RetailerNames>();
for (int rnIdx = 0; rnIdx < retailernamesChildNL
.getLength(); rnIdx++) {
Node retailernamesChildNode = retailernamesChildNL
.item(rnIdx);
RetailerNames retailernamesBean = null;
Element retailernamesChildElmt = null;
if (retailernamesChildNode
.hasAttributes()) {
retailernamesBean = new RetailerNames();
retailernamesChildElmt = (Element) retailernamesChildNode;
retailernamesBean
.setmProductType(retailernamesChildElmt
.getAttribute("name"));
RetailerNameAL
.add(retailernamesBean);
我在这里得到的零售商名称列表取决于所选的产品。
我的查询是:
我必须将这些名单列表传递给另一个活动,具体取决于productid。
List<String> shoplist = new ArrayList<String>();
shoplist.add(product.getString(TAG_SHOP));
shoplist.addAll(HomePage.retailerNameBean
.getmProductType()
+ ",");
ArrayAdapter<String> shopadapter = new ArrayAdapter<String>(EditWatchListProducts.this,
android.R.layout.simple_spinner_item, shoplist);
但是我正确地得到了第一个项目。这是获得该列表上的空项目:
请给我解决这些问题的方法......我如何在选择的类别上获得该arraylist depa的Retailername列表。
答案 0 :(得分:0)
尝试此代码......
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class XMLParsingDOMExample extends Activity {
ArrayList<String> title;
ArrayList<String> description;
ItemAdapter adapter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
title = new ArrayList<String>();
description = new ArrayList<String>();
try {
URL url = new URL(Your URL);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
Log.v("title--", ""+ ((Node) nameList.item(0)).getNodeValue());
title.add(""+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("description");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
Log.v("description--", ""+ ((Node) websiteList.item(0)).getNodeValue());
description.add(""+ ((Node) websiteList.item(0)).getNodeValue());
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
adapter1 = new ItemAdapter(this);
list.setAdapter(adapter1);
}
class ItemAdapter extends BaseAdapter {
final LayoutInflater mInflater;
private class ViewHolder {
public TextView title_text;
public TextView des_text;
}
public ItemAdapter(Context context) {
// TODO Auto-generated constructor stub
super();
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//@Override
public int getCount() {
return title.size();
}
//@Override
public Object getItem(int position) {
return position;
}
//@Override
public long getItemId(int position) {
return position;
}
//@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = mInflater.inflate(R.layout.mainpage_listitem_activity, parent, false);
holder = new ViewHolder();
holder.title_text = (TextView) view.findViewById(R.id.title_text);
holder.des_text = (TextView) view.findViewById(R.id.des_text);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title_text.setText(""+title.get(position));
holder.des_text.setText(""+Html.fromHtml(description.get(position)));
return view;
}
}
}
而不是项目使用零售商而不是标题&amp; description使用Retailername
答案 1 :(得分:0)
以下是从XML文件中获取值的解决方案
我的XML文件包含我想要的值,文件名是Version.xml。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="Version" class="com.XXX.domain.web.permissions.Version">
<property name="Version" value="2013061101"></property>
<property name="Release" value="0.5"></property>
</bean>
</beans>
现在我需要一个Bean类来映射XML中的值,在包中命名为Version com.XXX.domain.web.permissions
它包含以下代码
package com.XXX.domain.web.permissions;
import javax.faces.bean.ManagedBean;
public class Version {
private String Version;
private String Release;
public Version() {
}
public String getVersion() {
return Version;
}
public void setVersion(String version) {
Version = version;
}
public String getRelease() {
return Release;
}
public void setRelease(String release) {
Release = release;
}
}
现在XML将值映射到Bean类,您可以使用它从任何地方获取值。
ApplicationContext getBean= new ClassPathXmlApplicationContext("META-INF/Version.xml");
Version ver=(Version)getBean.getBean("Version");
return ver.getRelease();