views:

33

answers:

0

Hello,

I have a remote XML that I want to parse, but for some reason, my ListField only shows 1 row. I tried debugging and saw the count for my Vector, which is clearly more than 1.

Here is my code:

package com.myapp.blackberry;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class HomeScreen extends UiApplication {
    public static void main(String[] args)
    {
        HomeScreen theApp = new HomeScreen();
        theApp.enterEventDispatcher();
    }

    public HomeScreen()
    {
        pushScreen(new HomeNavigationScreen());
    }
}

class HomeNavigationScreen extends MainScreen
{
    private ListField navField;
    private Vector navItems;
    Connection _connectionthread;

    public HomeNavigationScreen()
    {

        setTitle("my app");

        navItems = new Vector();
        navField = new ListField();

        ListCallback callBack = new ListCallback();
        navField.setCallback(callBack);

        _connectionthread = new Connection();
        _connectionthread.start();


        add(navField);

    }

    private class ListCallback implements ListFieldCallback
    {
        public void drawListRow(ListField list, Graphics g, int index, int y, int w)
        {
            String text = (String) this.get(list, index);

            g.drawText(text, 0, y, 0, w);
        }

        public Object get(ListField listField, int index) {
            return navItems.elementAt(index);
        }

        public int getPreferredWidth(ListField listField) {
            return Display.getWidth();
        }

        public int indexOfList(ListField listField, String prefix, int start) {
            return navItems.indexOf(prefix, start);
        }
    }

    private class Connection extends Thread{
        public Connection(){
            super();
        }

        public void run(){
            // define variables later used for parsing
            Document doc;
            StreamConnection conn;
            try{
                conn = (StreamConnection)Connector.open("http://localhost/events.xml");
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                docBuilder.isValidating();
                doc = docBuilder.parse(conn.openInputStream());
                doc.getDocumentElement().normalize();
                NodeList list = doc.getElementsByTagName("Event");

                for (int i=0;i<list.getLength();i++){

                    Node currNode = list.item(i);

                    if (currNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element currElement = (Element) currNode;

                        NodeList titleList = currElement.getElementsByTagName("title");
                        Element titleElem = (Element) titleList.item(0);
                        NodeList titleNode = titleElem.getChildNodes();
                        String title = ((Node) titleNode.item(0)).getNodeValue();

                        navItems.addElement(title);
                        navField.insert(i);
                    }   
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

am I doing something wrong? I am very confused.