2012-06-04 51 views
0

我是黑莓開發應用程序的新手。我嘗試將所有xml解析數據存儲到一個對象,並將它們設置爲一個向量。Blackberry - 如果addElement()不起作用?

public class XmlParser extends MainScreen { 
    Database d; 
    private HttpConnection hcon = null; 

    private Vector binN; 
    public Vector getBinN() { 
     return binN; 
    } 

    public void setBinN(Vector bin) { 
     this.binN = bin; 
    } 

    LabelField from; 
    LabelField ttl; 
    LabelField desc; 
    LabelField date; 

    public XmlParser() { 
     LabelField title = new LabelField("Headline News" ,LabelField.HCENTER|LabelField.USE_ALL_WIDTH); 
     setTitle(title); 

     try { 
      URI myURI = URI.create("file:///SDCard/Database/WebFeed.db"); 
      d = DatabaseFactory.open(myURI); 
      Statement st = d.createStatement("SELECT feed_url, feed_name FROM WebFeed"); 
      st.prepare(); 
      Cursor c = st.getCursor(); 
      while (c.next()) { 
       Row r = c.getRow(); 
       hcon = (HttpConnection)Connector.open(r.getString(0)); 
       hcon.setRequestMethod(HttpConnection.GET); 
         hcon.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); 
         hcon.setRequestProperty("Content-Length", "0"); 
         hcon.setRequestProperty("Connection", "close"); 
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder builder = factory.newDocumentBuilder(); 

       builder.isValidating(); 
       Document document = builder.parse(hcon.openInputStream()); 

       Element rootElement = document.getDocumentElement(); 
       rootElement.normalize(); 

       NodeList list = document.getElementsByTagName("item"); 

       int i=0; 
       while (i<10){ 
        Node item = list.item(i); 
        if(item.getNodeType() != Node.TEXT_NODE) { 
         NodeList itemChilds = item.getChildNodes(); 
         int j=0; 
         while (j<10){ 
          Node detailNode = itemChilds.item(j); 
          if(detailNode.getNodeType() != Node.TEXT_NODE) {         
           if(detailNode.getNodeName().equalsIgnoreCase("title")) { 
            ttl = new LabelField(getNodeValue(detailNode)) { 
             public void paint(Graphics g) { 
              g.setColor(Color.BLUE); 
              super.paint(g); 
             } 
            }; 
            from = new LabelField(r.getString(1), LabelField.FIELD_RIGHT|LabelField.USE_ALL_WIDTH); 
            ttl.setFont(Font.getDefault().derive(Font.BOLD)); 
            from.setFont(Font.getDefault().derive(Font.BOLD)); 
            add (from); 
            add (ttl); 
           } else if(detailNode.getNodeName().equalsIgnoreCase("description")) { 

            desc = new LabelField(getNodeValue(detailNode), 0, 70, USE_ALL_WIDTH); 
            add(desc); 
           } else if(detailNode.getNodeName().equalsIgnoreCase("dc:date")) { 
            date = new LabelField(getNodeValue(detailNode), 11, 5, USE_ALL_WIDTH) { 
             public void paint(Graphics g) { 
              g.setColor(Color.ORANGE); 
              super.paint(g); 
             } 
            }; 
            add(date); 
            add(new SeparatorField()); 
           } else if(detailNode.getNodeName().equalsIgnoreCase("pubDate")) { 
            date = new LabelField(getNodeValue(detailNode), 0, 22, USE_ALL_WIDTH) { 
             public void paint(Graphics g) { 
              g.setColor(Color.ORANGE); 
              super.paint(g); 
             } 
            }; 
            add(date); 
            add(new SeparatorField()); 
           } else { 
            System.out.println("not the node"); 
           } 
          } else { 
           System.out.println("not text node"); 
          } 
          j++; 
         } 
        } 
        i++; 
        BinNews bin = new BinNews(); 
        bin.setProv(from.getText()); 
        bin.setTitle(ttl.getText()); 
        bin.setDesc(desc.getText()); 
        bin.setDate(date.getText()); 
        binN.addElement(bin); 
       } 
       setBinN(binN); 
      } 
      //setBinN(binN); 
      st.close(); 
      d.close(); 
     } catch (Exception e) { 
      add (new LabelField(e.toString(),LabelField.HCENTER|LabelField.USE_ALL_WIDTH)); 
      System.out.println(e.toString()); 
     } 
    } 

    public String getNodeValue(Node node) { 
     NodeList nodeList = node.getChildNodes(); 
     Node childNode = nodeList.item(0); 
     return childNode.getNodeValue(); 
    } 
} 

我嘗試叫BinNews對象存儲的所有數據,到一個叫做BINN載體。但是當我進行調試時,我發現BinN具有空值,因爲「binN.addElement(bin)」不起作用。 請指教。

回答

0

首先,直到while(i < 10)循環完成後才真正調用setBinN。所以,當你說binN.addElement(bin)時,binN將爲空。

但是,您的setBinN(binN)調用沒有意義,因爲您傳遞的是binN,然後將其設置爲自己,而不會執行任何操作。

你可以做的是在構造函數的頂部有binN = new Vector();,然後它不會在以後爲空。如果將BinNews對象直接添加到binN,我認爲稍後將不需要setBinN調用。

+0

是的,這是答案,我昨天才意識到它。謝謝 :) – grcnatalia