fetching values from arraylist and give it to hashmap
I had parsed data from XML file.and stored it in an arraylist.and i want to copy the contents of arraylist into a hashmap.i m giving dis code.
ArrayList<String> items=new ArrayList<String>();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
开发者_如何转开发 HashMap<String, String> map = new HashMap<String, String>();
try {
XmlPullParser xpp=getResources().getXml(R.xml.call_record_request_structure);
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType()==XmlPullParser.START_TAG) {
String name=xpp.getName();
String temp="productName";
//items.add(name);
if (name.equals(temp)) {
String pro=xpp.nextText();
items.add(pro);
int count=items.size();
for(int i=0;i<count;i++) {
map = new HashMap<String, String>();
map.put("product", items.get(i));
mylist.add(map);
}
}
}
xpp.next();
}
}
catch (Throwable t) {
Toast
.makeText(this, "Request failed: "+t.toString(), 4000)
.show();
}
SimpleAdapter mHistory = new SimpleAdapter(this,mylist , R.layout.prodlist_supp,
new String[] {"product"},
new int[] {R.id.products});
listview.setAdapter(mHistory);
but when i run the code it s crashing.can anyone help me
Try this: add map after the loop ends.
map = new HashMap<String, String>();
for(int i=0;i<count;i++) {
map.put("product", items.get(i));
}
mylist.add(map);
if problem persists show your stacktrace.
精彩评论