2011-08-23 73 views
0

我使用下面的代碼用Android的人XMLParsing例如用於解析XML文件,它是工作完全正常..XML解析,並顯示在列表視圖中的結果

但檢索的TextView數據我想把這個數據在一個ListView ..

我嘗試通過創建CustomAdapter並通過將活動更改爲ListActivity ..但沒有工作..我只能看到一個空白的屏幕..任何人都可以幫助我做這...

謝謝!!

 package com.androidpeople.xml.parsing; 

    import java.net.URL; 
    import javax.xml.parsers.SAXParser; 
    import javax.xml.parsers.SAXParserFactory; 
    import org.xml.sax.InputSource; 
    import org.xml.sax.XMLReader; 
    import android.app.Activity; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.widget.LinearLayout; 
    import android.widget.TextView; 

    public class XMLParsingExample extends Activity { 

/** Create Object For markerList Class */ 
mymarkers markerList =null; 

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

    /** Create a new layout to display the view */ 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(1); 



    /** Create a new textview array to display the results */ 
    TextView name[]; 
    TextView address[]; 
    TextView city[]; 
    try { 

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

     /** Send URL to parse XML Tags */ 
     URL sourceUrl = new URL("xxxxxxxxx"); 

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

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

    /** Get result from MyXMLHandler SitlesList Object */ 
    markerList = MyXMLHandler.markerList; 

    /** Assign textview array lenght by arraylist size */ 


    name = new TextView[markerList.getName().size()]; 
    address = new TextView[markerList.getaddress().size()]; 
    city = new TextView[markerList.getCity().size()]; 
    /** Set the result text in textview and add it to layout */ 
    for (int i = 0; i < markerList.getName().size(); i++) { 
     name[i] = new TextView(this); 
     name[i].setText("Name = "+markerList.getName().get(i)); 

     address[i] = new TextView(this); 
     address[i].setText("address = "+markerList.getaddress().get(i)); 

     city[i] = new TextView(this); 
     city[i].setText("city = "+markerList.getCity().get(i)); 
     System.out.println("count33..."+markerList.getCity().size()); 
     city[i].setBackgroundColor(Color.WHITE); 
     layout.addView(name[i]); 
     layout.addView(address[i]); 
     layout.addView(city[i]); 

     //layout.addView(border[i]); 
    } 

    /** Set the layout view to display */ 
    setContentView(layout); 

} 
} 
+0

我認爲你需要先學習ListView的ListView和自定義adpater。那麼你很容易實現這一點。 –

回答

0

我幾天前回答了一個非常類似的問題,您可能需要檢查。您可以檢查問題Android: Sax parsing to a listview並檢查接受的答案。按照指示,直到getView()部分,你應該沒問題。

在這裏總之就是你需要做什麼:

  • 使用DOM而非SAX,它是ListView控件
  • 簡單,完美實現一個可擴展BaseAdapter通過根元素到該適配器的適配器
  • 在適配器中實現getView以添加所需的TextView

我再次在Android: Sax parsing to a listview中規劃了步驟。讓我知道如果您有任何進一步的問題

+0

你的回答基本上總結,幫助我得到理想的輸出....謝謝.. –

相關問題