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 7078231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:31:16+00:00 2026-05-28T06:31:16+00:00

I am in the middle of converting a listview example into a program that

  • 0

I am in the middle of converting a listview example into a program that operates by reading an xml file and then populate a list view with its contents. I seem to have hit a snag though. I’m not sure what to do with the Adapter I made for the first example and how to use it in this new program, any pointers would be appreciated.
Heres the main

Phonebook phonebook = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView list = (ListView) findViewById(R.id.ListView01);
    list.setClickable(true);

    final List<Phonebook> listOfPhonebook = new ArrayList<Phonebook>();
    PhonebookAdapterView adapter = new PhonebookAdapterView(this, listOfPhonebook);

    list.setAdapter(adapter);

    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
         InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(is.getByteStream()));

    } catch (Exception e) {
        System.out.println("XML Parsing Exception = " + e);
    }


    phonebook = MyXMLHandler.phonebook;
    list.setAdapter(adapter);

Heres the xml handler

    Boolean currentElement = false;
    String currentValue = null;
    public static Phonebook phonebook = null;

    public static Phonebook getPhonebook() {
        return phonebook;
    }

    public static void setPhonebook(Phonebook p) {
        MyXMLHandler.phonebook = p;
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        if (localName.equals("maintag"))
        {
            /** Start */
            phonebook = new Phonebook();
        }
    }

    /** Called when tag closing ( ex:- <name>AndroidPeople</name>
     * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        /** set value */
        if (localName.equalsIgnoreCase("name"))
            phonebook.setName(currentValue);
        else if (localName.equalsIgnoreCase("phone"))
            phonebook.setPhone(currentValue);
        else if (localName.equalsIgnoreCase("Mail"))
            phonebook.setMail(currentValue);

    }

    /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
     * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }

}

And the adapter view, which is the same back when it was a simple listview file. Im not sure how to factor in xml though.

public class PhonebookAdapterView extends BaseAdapter {

private Context context;
private List<Phonebook> phonebook;

 public PhonebookAdapterView(Context theContext, List<Phonebook> theListPhonebook) {
        context = theContext;
        phonebook = theListPhonebook;
    }


@Override
public int getCount() {
    return phonebook.size();
}
@Override
public Object getItem(int position) {
    return phonebook.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
     Phonebook entry = phonebook.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.phone_row, null);
        }
        TextView tvContact = (TextView) convertView.findViewById(R.id.tvContact);
        tvContact.setText(entry.getName());

        TextView tvPhone = (TextView) convertView.findViewById(R.id.tvMobile);
        tvPhone.setText(entry.getPhone());

        TextView tvMail = (TextView) convertView.findViewById(R.id.tvMail);
        tvMail.setText(entry.getMail());

      return convertView;
}

}

EDIT

MyXMLHAndler

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static List<Phonebook> phonebook = new ArrayList<Phonebook>();

    public static List<Phonebook> getPhonebook() {
        return phonebook;
    }

    public static void setPhonebook(List<Phonebook> p) {
        MyXMLHandler.phonebook = p;
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        if (localName.equals("maintag"))
        {
            /** Start */
            phonebook = new ArrayList<Phonebook>();
        }
    }

    /** Called when tag closing ( ex:- <name>AndroidPeople</name>
     * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        /** set value */
        if (localName.equalsIgnoreCase("name"))
            ((Phonebook) phonebook).setName(currentValue);
        else if (localName.equalsIgnoreCase("phone"))
            ((Phonebook) phonebook).setPhone(currentValue);
        else if (localName.equalsIgnoreCase("Mail"))
            ((Phonebook) phonebook).setMail(currentValue);

    }

    /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
     * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }

}

Second Edit

public List<Phonebook> phonebook = new ArrayList<Phonebook>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView list = (ListView) findViewById(R.id.ListView01);
    list.setClickable(true);

    final List<Phonebook> listOfPhonebook = MyXMLHandler.getPhonebook();
    PhonebookAdapterView adapter = new PhonebookAdapterView(this, listOfPhonebook);
    list.setAdapter(adapter);

    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
         InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(is.getByteStream()));

    } catch (Exception e) {
        System.out.println("XML Parsing Exception = " + e);
    }  

XMLHandler

    public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public  List<Phonebook> phonebook = new ArrayList<Phonebook>();

    public List<Phonebook> getPhonebook() {
        return phonebook;
    }

    public void setPhonebook(List<Phonebook> p) {
        phonebook = p;
    }

    int entryCount;

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        if (localName.equals("maintag"))
        {
            /** Start */
            Phonebook pb = new Phonebook();
            getPhonebook().add(pb);
            entryCount++;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
  /** set value */
         if (localName.equalsIgnoreCase("name"))
                ((Phonebook)phonebook.get(entryCount)).setName(currentValue);
            if (localName.equalsIgnoreCase("phone"))
                ((Phonebook)phonebook.get(entryCount)).setPhone(currentValue);
            if (localName.equalsIgnoreCase("Mail"))
                ((Phonebook)phonebook.get(entryCount)).setMail(currentValue);
}


    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }
  • 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-05-28T06:31:16+00:00Added an answer on May 28, 2026 at 6:31 am

    There’re quite a few things to do to make it work.

    First of all you will have to create your phonebook list by reading an XML file
    and putting a data into a List of the MyXMLHandler class
    your

    public static Phonebook phonebook = null;
    

    should be

    public static List<Phonebook> phonebook = new ArrayList<Phonebook>();
    

    and populated accordingly within your XMLHandler class
    and after that you set this list into your adapter –

    MyAdapter.setList(phonebook); // your method inside adapter
    

    Then you’re using the Phonebook objects from this list
    within your getView() class to populate your TextViews and addView(TextView) them
    into your ConvertView object and return it.

    I’ve made an example list application in this post how can i select and kill multiple application
    or there’s a bunch of posts on how to create a listview.

    UPDATED:

    in your XMLHandler class
    Remove the statics from phonebook public static List<Phonebook> phonebook as well
    as from the set/getPhonebook() methods
    you are NEWing the class and use the myXMLHandler.getPhonebook(); in your activity class.

    assuming you have the following xml

    <phonebook>
    <entry>
    <name>abc</name>
    <phone>def</phone>
    <mail>ghi</mail>
    </entry>
    <entry>
    <name>abc1</name>
    <phone>def2</phone>
    <mail>ghi3</mail>
    </entry>
    </phonebook>
    

    Working code:

    import android.app.Activity;
    import android.content.Context;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.*;
    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 javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListActivity extends Activity {
    
    
        ListView lv;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LinearLayout ll = new LinearLayout(this);
            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
    
            ll.setLayoutParams(lp1);
            ll.setOrientation(1);
            lv = new ListView(this);
            MyXMLHandler myXMLHandler = readXML();
    
            List<Phonebook> listOfPhonebook = myXMLHandler.getPhonebook();
            //adapter = new TaskListAdapter(this);
            MyListAdapter adapter = new MyListAdapter(this, listOfPhonebook);
            lv.setAdapter(adapter);
            ll.addView(lv);
    
            setContentView(ll);
    
        }
    
        private MyXMLHandler readXML() {
            MyXMLHandler myXMLHandler = null;
            try {
    
                /** Handling XML */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
    
                /** Send URL to parse XML Tags */
                InputSource is = new InputSource(getResources().openRawResource(R.raw.a));
    
                /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                myXMLHandler = new MyXMLHandler();
                xr.setContentHandler(myXMLHandler);
                xr.parse(new InputSource(is.getByteStream()));
    
            } catch (Exception e) {
                System.out.println("XML Parsing Exception = " + e);
            }
            return myXMLHandler;
        }
    
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    
    
        class MyListAdapter extends BaseAdapter {
    
            private static final String TAG = "TaskListAdapter";
    
    
            List<Phonebook> phonebook;
            Context context;
    
            public MyListAdapter(Context context, List<Phonebook> phonebook) {
                this.phonebook = phonebook;
                Log.d(TAG, "created new task list adapter");
                this.context = context;
            }
    
            public int getCount() {
                return phonebook.size();
            }
    
            public Phonebook getItem(int position) {
                return phonebook.get(position);
            }
    
            public long getItemId(int position) {
                return position;
            }
    
    
            public View getView(final int position, View convertView, ViewGroup parent) {
                RelativeLayout rl = new RelativeLayout(context);
                TextView textPid = new TextView(context);
                textPid.setId(222222);
                textPid.setText(getItem(position).getName());
    
                TextView textName = new TextView(context);
                textName.setId(333333);
                textName.setText(getItem(position).getEmail());
    
    
                TextView textEmail = new TextView(context);
                textEmail.setId(444444);
                textEmail.setText(getItem(position).getEmail());
    
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
    
                lp.setMargins(1, 0, 0, 0);
                rl.addView(textName, lp);
    
                RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp1.addRule(RelativeLayout.RIGHT_OF, textName.getId());
                lp1.setMargins(1, 0, 0, 0);
                rl.addView(textPid, lp1);
    
    
                RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp2.addRule(RelativeLayout.RIGHT_OF, textPid.getId());
                lp2.setMargins(1, 0, 0, 0);
                rl.addView(textEmail, lp2);
    
    
                return rl;
            }
    
        }
    
        class MyXMLHandler extends DefaultHandler {
    
            Boolean currentElement = false;
            String currentValue = null;
            public List<Phonebook> phonebook = new ArrayList<Phonebook>();
            int entryCount = 0;
    
            public List<Phonebook> getPhonebook() {
                return phonebook;
            }
    
            public void setPhonebook(List<Phonebook> p) {
                phonebook = p;
            }
    
    
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    
                currentElement = true;
    
                if (localName.equals("entry")) {
                    /** Start */
                    Phonebook pb = new Phonebook();
                    getPhonebook().add(pb);
    
                }
            }
    
            @Override
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {
                /** set value */
                if (localName.equalsIgnoreCase("name"))
                    (phonebook.get(entryCount)).setName(currentValue);
                if (localName.equalsIgnoreCase("phone"))
                    (phonebook.get(entryCount)).setPhone(currentValue);
                if (localName.equalsIgnoreCase("mail"))
                    (phonebook.get(entryCount)).setEmail(currentValue);
    
                if (localName.equalsIgnoreCase("entry")) entryCount++;
            }
    
    
            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
    
                if (currentElement) {
                    currentValue = new String(ch, start, length);
                    currentElement = false;
                }
    
            }
    
        }
    
    }
    

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

Sidebar

Related Questions

My company is in the middle of converting from CVS over to git. We've
I've noticed that most of the HTML/XML/HAML that gets generated from plugins uses 2
PROBLEM I have a nested PHP array that I need to populate from flat
In the middle of converting VB6 code to VB.NET, I need to replace the
I have been tasked with converting several php classes into java classes, which is
In the above code the calendar that is in the middle is actually a
I'm in the middle of a significant effort to introduce NHibernate into our code
I'm in the middle of converting a 10-year-old Java EE application to more modern
Clicking the middle mouse button (aka: mouse wheel) and then moving the mouse down
We are in a middle of an migration process of converting EJB2.1 entity beans

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.