Xml Parsing to Custom ListView

Tutorials with advanced 'difficulty' and more Lines of Code.

Xml Parsing to Custom ListView

Postby stelioschar » Fri Sep 24, 2010 9:06 am

Hello to all,

Does anyone has a tutorial that refers to XML Parsing and passing the data to a Custom ListView?

I have an XML file that i want to parse it and after that i want to create a custom ListView with this data.
I've already found a DOM Parser example http://www.ibm.com/developerworks/library/x-android/
and a tutorial on how to create a Custom ListView http://www.softwarepassion.com/android- ... -adapters/

If you have an advice how to do this would be very useful.

Thanks for you time.
Junior Developer
Junior Developer
Posts: 18
Joined: Thu Aug 19, 2010 9:18 am


Re: Xml Parsing to Custom ListView

Postby wskruse » Mon Sep 27, 2010 9:43 pm

I actually had the same task that I had to figure out. I end up using a SAX Parser, but I assume that it looks similar to the DOM parser.

Here is how I went about doing this:

I parsed the XML (using the saxparser and a custom handler) into a Map with the key being the element name, and the value an array list of the data in the xml file.

I then passed the map and the context to the adapter where you override the getCount (by passing it the size of one of the arraylists in the map) and getView methods.

As an example (and if anyone else has a better way of doing this please let me know)

Here is the handler class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class XMLHandler extends DefaultHandler {
  2.         static Map<String, ArrayList<String>> parsedXML = new HashMap<String, ArrayList<String>>();
  3.         static ArrayList<String> keyList = new ArrayList<String>();
  4.         StringBuilder buffer = new StringBuilder();
  6.         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  7.                 buffer.setLength(0);
  8.                 buffer.trimToSize();
  9.                 if (!keyList.contains(localName)) {
  10.                         keyList.add(localName);
  11.                         parsedXML.put(localName, new ArrayList<String>());
  12.                 }
  13.         }
  14.         public void characters(char ch[], int start, int length) {
  15.                 buffer.append(ch, start, length);
  16.         }
  17.         public void endElement(String namespaceURI, String localName, String qName) {
  18.                 String extractedString = buffer.toString();
  19.                 parsedXML.get(localName).add(extractedString);
  20.         }
  21. }
Parsed in 0.017 seconds, using GeSHi

and here is the getView method of the adapter:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3.                 View containerLayout = convertView;
  4.                 if (containerLayout == null) { //Inflate the view on first use
  5.                         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  6.                         containerLayout = inflater.inflate(R.layout.mainrow, null);
  7.                 }
  8.                 ImageView iconView = (ImageView) containerLayout.findViewById(R.id.mainIcon);
  9.                 TextView labelView = (TextView) containerLayout.findViewById(R.id.mainText);
  10.                 String key = (String) MainAdapter.keyArray[position];
  11.                 SharedPreferences itemPrefs = mContext.getSharedPreferences(key, 0);
  12.                 labelView.setText(itemPrefs.getString("title", "Sorry No Title Was Found"));
  13.                 containerLayout.setTag(R.id.title, itemPrefs.getString("title", "Sorry no Title was found"));
  14.                 containerLayout.setTag(R.id.url, itemPrefs.getString("url", null));
  15.                 Resources res = mContext.getResources();
  16.                 iconView.setImageDrawable(res.getDrawable(R.drawable.icon));
  17.                 return containerLayout;
  18.         }
Parsed in 0.012 seconds, using GeSHi

Hope this helps.
Once Poster
Once Poster
Posts: 1
Joined: Mon Aug 09, 2010 5:38 pm
Location: Washington, DC

Re: Xml Parsing to Custom ListView

Postby stelioschar » Wed Sep 29, 2010 1:56 pm

Hi wskruse,

Can you give us the structure of your xml file that you're parsing??

Also can you explain the way, you create the "list elements", once you've already read them from the xml?
And i'm refering to this code
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. private void getOrders(){
  2.           try{
  3.               m_orders = new ArrayList<Order>();
  4.               Order o1 = new Order();
  5.               o1.setOrderName("SF services");
  6.               o1.setOrderStatus("Pending");
  7.               Order o2 = new Order();
  8.               o2.setOrderName("SF Advertisement");
  9.               o2.setOrderStatus("Completed");
  10.               m_orders.add(o1);
  11.               m_orders.add(o2);
  12.                  Thread.sleep(2000);
  13.               Log.i("ARRAY", ""+ m_orders.size());
  14.             } catch (Exception e) {
  15.               Log.e("BACKGROUND_PROC", e.getMessage());
  16.             }
  17.             runOnUiThread(returnRes);
  18.         }
Parsed in 0.002 seconds, using GeSHi

that exists in this Custom ListView Tutorial http://www.softwarepassion.com/android- ... -adapters/.

You've already understood that i'm rookie on java and any help, would be useful.

Thanks in advance,

Junior Developer
Junior Developer
Posts: 18
Joined: Thu Aug 19, 2010 9:18 am


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests