ListView Data spliting into another row after Sax parsing

爷,独闯天下 提交于 2019-12-01 01:24:49
Luksprog

The code below(modifying your MyHandler class) should keep your node text together:

    // a field in the MyHandler class:
    boolean mIsSegment = false;
    // ...
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        super.characters(ch, start, length);
        if (is_sno) {
            al_sno.add(new String(ch, start, length));
        } else if (is_sname) {
            if (!mIsSegment) {
                al_sname.add(new String(ch, start, length));
            } else {
                al_sname.set(al_sname.size() - 1,
                        al_sname.get(al_sname.size() - 1)
                                + new String(ch, start, length));
            }
            mIsSegment = true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // TODO Auto-generated method stub
        super.endElement(uri, localName, name);

        if (localName.equals("ID")) {
            is_sno = false;
        } else if (localName.equals("Name")) {
            is_sname = false;
            mIsSegment = false;
        }

    }
    // ...

The SAXParser is breaking the name text in multiple pieces and you're adding each of this pieces as a single item in the list. Check this answer. Also there is no reason to call setListdapter twice(just call it once at the end of the onCreate method).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!