本文介绍了Android的XMLPARSE来的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对不起,如果我做的事情混淆。我编辑了整个问题。
我用XMLPARSER为我的网址是对我的网络服务器托管。
我想从解析 url.add(图)我的URL列表;
到 intent.putExtra(Extra.IMAGES,imageUrls);
,这样我可以检索我的XMLPARSER的URL。请指导我如何使用codeS做到这一点。我试图显示误差
public class HomeActivity extends BaseActivity {
static final String URL = "http://*******";
static final String KEY_LIST = "list";
static final String KEY_THUMB_URL = "thumb_url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_home);
ArrayList<HashMap<String, String>> url = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_LIST);
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
url.add(map); // <***** Set the results from here to imageUrls
}
imageUrls = (String[]) url.toArray(new String[0]); // <----***Error****
}
public void onImageGridClick(View view) {
Intent intent = new Intent(this, ImageGridActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
startActivity(intent);
}
public void onImagePagerClick(View view) {
Intent intent = new Intent(this, ImagePagerActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
startActivity(intent);
}
}
解决方案
Try this code
public class MainActivity extends BaseActivity {
String[] imageUrls;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_home);
ArrayList<String> url = new ArrayList<String>();
try {
URL url_link = new URL("http:******");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url_link.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("list");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element)node;
NodeList nameList = fstElmnt.getElementsByTagName("thumb_url");
Element nameElement = (Element)nameList.item(0);
nameList = nameElement.getChildNodes();
url.add(nameList.item(0).getNodeValue());
}
imageUrls = (String[]) url.toArray(new String[0]);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
public void onImageGridClick(View view) {
Intent intent = new Intent(this, ImageGridActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
startActivity(intent);
}
public void onImagePagerClick(View view) {
Intent intent = new Intent(this, ImagePagerActivity.class);
intent.putExtra(Extra.IMAGES, imageUrls);
startActivity(intent);
}
}
这篇关于Android的XMLPARSE来的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!