ListView的数据劈裂成SAX解析后,另一行数据、ListView、SAX

2023-09-06 06:30:02 作者:离城怎回。

我收到错误的列表视图中的数据。我使用SAX解析,以获取来自网络服务的数据。服务好。但我不知道是哪里的错误是? 在这里,我的code。

I am getting wrong listview data. I am using SAX parsing to get data from Web-service. Service is good. but i don't know where the mistake is? Here my code.

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;



import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SaxParserDemoActivity extends ListActivity {
    /** Called when the activity is first created. */
    ArrayList<String> al_sno=new ArrayList<String>();
    ArrayList<String> al_sname=new ArrayList<String>();
    ArrayList<String> al_sclass=new ArrayList<String>();
    ArrayList<String> al_sphno=new ArrayList<String>();
    ArrayList<String> al_semail=new ArrayList<String>();

    SAXParserFactory spf;
    SAXParser sp;
    XMLReader xr;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try{


            spf=SAXParserFactory.newInstance();
            sp=spf.newSAXParser();
            xr=sp.getXMLReader();

            URL sourceUrl = new URL(
            "http://xxxxxxxxxxxxxxxxxxx");


            MyHandler mh=new MyHandler();
            xr.setContentHandler(mh);

            xr.parse(new InputSource(sourceUrl.openStream()));

            //.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
            setListAdapter(new MyAdapter());





        }
        catch(Exception e){}

        setListAdapter(new MyAdapter());
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return al_sclass.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            LayoutInflater li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            View v=li.inflate(R.layout.second, null);

            TextView tv1=(TextView)v.findViewById(R.id.text1);
            tv1.setText(al_sno.get(arg0));

            TextView tv2=(TextView)v.findViewById(R.id.text2);
            tv2.setText(al_sname.get(arg0));



            return v;
        }

    }

    class MyHandler extends DefaultHandler{
        boolean is_sno=false;
        boolean is_sname=false;


        @Override
        public void startDocument() throws SAXException {
            // TODO Auto-generated method stub
            super.startDocument();
        }

        @Override
        public void startElement(String uri, String localName, String name,
                Attributes attributes) throws SAXException {
            super.startElement(uri, localName, name, attributes);
            if(localName.equals("ID")){
                is_sno=true;
            }
            else if(localName.equals("Name")){
                is_sname=true;
            }

        }

        @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){
                al_sname.add(new String(ch,start,length));
            }

        }

        @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;
            }

        }

        @Override
        public void endDocument() throws SAXException {
            // TODO Auto-generated method stub
            super.endDocument();
        }
    }
}

我的输出画面 。

My output screen .

U可以在这里看到阿拉巴马州A和M被劈裂成3行

U can see here "Alabama A&M" is spliting into 3 rows.

我的logcat的是

11-27 11:56:15.516: D/PhoneWindow(429): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@40549d60 has no id.
11-27 11:56:15.976: D/dalvikvm(429): GC_CONCURRENT freed 218K, 50% free 2958K/5831K, external 1064K/1413K, paused 17ms+4ms
11-27 11:56:17.876: V/tea(429): Air Force
11-27 11:56:17.929: V/tea(429): Akron
11-27 11:56:17.946: V/tea(429): Alabama
11-27 11:56:17.986: V/tea(429): Alabama A
11-27 11:56:17.996: V/tea(429): &
11-27 11:56:18.036: V/tea(429): M
11-27 11:56:18.046: V/tea(429): Alabama State
11-27 11:56:18.066: V/tea(429): Albany
11-27 11:56:18.156: V/tea(429): Air Force
11-27 11:56:18.166: V/tea(429): Akron
11-27 11:56:18.196: V/tea(429): Alabama
11-27 11:56:18.229: V/tea(429): Alabama A
11-27 11:56:18.246: V/tea(429): &
11-27 11:56:18.274: V/tea(429): M
11-27 11:56:18.286: V/tea(429): Alabama State
11-27 11:56:18.326: V/tea(429): Albany
11-27 11:56:18.376: V/tea(429): Air Force
11-27 11:56:18.396: V/tea(429): Akron
11-27 11:56:18.426: V/tea(429): Alabama
11-27 11:56:18.456: V/tea(429): Alabama A
11-27 11:56:18.467: V/tea(429): &
11-27 11:56:18.504: V/tea(429): M
11-27 11:56:18.526: V/tea(429): Alabama State
11-27 11:56:18.636: V/tea(429): Albany
11-27 11:56:18.756: D/dalvikvm(429): GC_CONCURRENT freed 173K, 48% free 3188K/6023K, external 1056K/1413K, paused 19ms+6ms
11-27 11:56:19.097: I/ActivityManager(60): Displayed com.ei.s/.Teamlist: +3s566ms

我的XML数据...

My XML Data is...

<Table diffgr:id="Table2" msdata:rowOrder="1">
    <ID>47</ID>
    <Name>Akron</Name>
    </Table>
    <Table diffgr:id="Table3" msdata:rowOrder="2">
    <ID>73</ID>
    <Name>Alabama</Name>
   </Table>
    <Table diffgr:id="Table4" msdata:rowOrder="3">
    <ID>356</ID>
    <Name>Alabama A&M</Name>
   </Table>
    <Table diffgr:id="Table5" msdata:rowOrder="4">
    <ID>187</ID>
    <Name>Alabama State</Name>
    </Table>

我得到正确的数据在web服务,但不是在Android应用程序。请帮我,我在哪里犯错吗?

I am getting correct data in WEB-Service but not in Android app. Please help me that where i am making mistake here?

推荐答案

以下(修改你的 MyHandler的类)应该保持你的节点文本在一起code:

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;
        }

    }
    // ...

的SAXParser 被打破多件名称的文字和您要添加的每个本作品为列表中的一个项目。检查这个答案。也没有理由叫 setListdapter 两次(只是在的onCreate 方法的最后调用一次)。

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).