开发者

fetching values from arraylist and give it to hashmap

开发者 https://www.devze.com 2023-02-20 06:53 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号