Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8905693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:27:36+00:00 2026-06-15T02:27:36+00:00

I am getting wrong listview data. I am using SAX parsing to get data

  • 0

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:

Screen.

You can see here “Alabama A&M” is splitting into 3 rows.

My logcat is

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

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>

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:27:37+00:00Added an answer on June 15, 2026 at 2:27 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jboss web service that is getting the wrong initial context. I
(Maybe I am doing something wrong but) I am getting awful read performance from
Im trying to convert unixtime to date but the results im getting are wrong
I'm trying to populate a listview from another class, but I'm geting this error:
I'm getting am AndroidRuntimeException when I click the menuButton using this custom ListView Adapter:
I am getting wrong column names when using union. Here is what i do,
I have a simple c# console application but i am getting wrong output why?
I have following code snippet but I am getting wrong output. class Program {
1) I am using a ListView to populate some 2 labels from database. The
I am getting some data from the net, which gets parsed with JSoup in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.