2013-04-17 15 views
0

我最近一直在研究Android ListViews,並且遇到了一些問題。我看了幾個教程,並得到了最基本的ListViews工作得很好。但是現在我想創建一個ListView來顯示一系列對象。我以爲我做的一切都是正確的。我的代碼不會產生任何錯誤,但我希望創建的列表根本不顯示。顯然,我不知道爲什麼。Android ListView不合作

這個應用程序的目的是編制一個氣象站和機場的列表,並顯示信息(例如名稱,ID#,座標等) - 所有這些都是從一個XML文檔解析幷包含在一個Arraylist由具有適當構造函數/ getters/setters的獨立類構建)。基本上,我會得到一個電臺列表 - 然後從該列表中 - 一個電臺名稱列表設置爲一個適配器。我的ListView應該只顯示每個站的名字,但無濟於事。

我測試了我的Parser和My Arrays。這一切正常,只是沒有顯示。根據所有的教程,我的邏輯應該是正確的,我的適配器。有沒有人有什麼建議?我覺得我正在用盡所有解決方案。我的代碼下面貼:

public class MainActivity extends Activity { 

//local variables 
    String station_id; 
    String state; 
    String station_name; 
    double latitude; 
    double longitude; 
    String html_url; 

//ArrayList<String> stationList = new ArrayList<String>(); 
public ArrayList<Station> stationList = new ArrayList<Station>(); 

private ListView stationName; 
private ArrayAdapter<String> arrayAdapter; 



//Method for DOM Parser 
    public void readXML(){ 

    try { 

     //new xml file and Read 
     File file1 = new File("src/fl_wx_index3.xml"); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(file1); 
     doc.getDocumentElement().normalize(); 

     NodeList nodeList = doc.getElementsByTagName("station"); 

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

      Node node = nodeList.item(i); 

      if(node.getNodeType() == Node.ELEMENT_NODE){ 

       final Element first = (Element) node; 

       station_id = first.getElementsByTagName("station_id").item(0).getTextContent(); 
       state = first.getElementsByTagName("state").item(0).getTextContent(); 
       station_name = first.getElementsByTagName("station_name").item(0).getTextContent(); 
       latitude = Double.parseDouble(first.getElementsByTagName("latitude").item(0).getTextContent()); 
       longitude = Double.parseDouble(first.getElementsByTagName("longitude").item(0).getTextContent()); 
       html_url = first.getElementsByTagName("html_url").item(0).getTextContent(); 

       //iterate thru list, returning names of each airport 
       stationList.add(new Station(station_id, state, station_name, latitude, longitude, html_url)); 

      } 

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

} 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Initialize the UI components 
    stationName = (ListView) findViewById(R.id.listView1); 

    //object for method call to read XML document 
     MainActivity activity1 = new MainActivity(); 
     activity1.readXML(); 

    //List to contain Weather station Names 
    final ArrayList<String> nameList = new ArrayList<String>(); 

    for (int i = 0; i < stationList.size(); ++i) { 
      nameList.add(stationList.get(i).getStationName()); 
     } 

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameList); 

    // By using setAdapter method, you plugged the ListView with adapter 
    stationName.setAdapter(arrayAdapter); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

這裏是XML的一個樣本:

<?xml version="1.0" encoding="UTF-8"?> 
<wx_station_index> 
    <credit>NOAA's National Weather Service</credit> 
    <credit_URL>http://weather.gov/</credit_URL> 
    <image> 
      <url>http://weather.gov/images/xml_logo.gif</url> 
      <title>NOAA's National Weather Service</title> 
      <link>http://weather.gov</link> 
    </image> 
    <suggested_pickup>08:00 EST</suggested_pickup> 
    <suggested_pickup_period>1140</suggested_pickup_period> 

<station> 
    <station_id>NFNA</station_id> 
    <state>FJ</state> 
    <station_name>Nausori</station_name> 
    <latitude>-18.05</latitude> 
    <longitude>178.567</longitude> 
    <html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url> 
    <rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url> 
    <xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url> 
</station> 

<station> 
    <station_id>KCEW</station_id> 
    <state>FL</state> 
      <station_name>Crestview, Sikes Airport</station_name> 
    <latitude>30.79</latitude> 
    <longitude>-86.52</longitude> 
      <html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url> 
      <rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url> 
      <xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url> 
</station> 

活動主要XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="115dp" 
    android:layout_toRightOf="@+id/textView1" > 
</ListView> 

+0

你爲什麼不使用'ListActity'?也許問題出現在你的佈局'activity_main.xml'中,發佈你的xml。 您不需要實例化'MainActivity'來調用'readXML()',只需從'onCreate()'調用它即可。 – rubenlop

+0

我已經發布了我的XML。我切換到ListActivity,我試着從OnCreate()中解析XML。現在它只是崩潰。 – David

回答

0

您的應用由於你的On而崩潰創建

而不是

MainActivity activity1 = new MainActivity(); 
activity1.readXML(); 

你真正應該呼籲只是

readXML(); 
+0

已更改。現在它沒有像以前一樣顯示。 – David

+0

另外,看着你的佈局,你的ListView1的邊距爲115dp。嘗試取出或設置爲更小的東西,如5dp。 – jwong

+0

終於認識到了這個問題。我只需要將xml移動到assets文件夾 - 然後設置爲構建路徑。然後使用getAssets()。 – David

0

第一:深呼吸,放鬆,你是不是唯一的一個(這是一種解脫自己),我對ListViews感到最沮喪,你真正需要的是稱爲自定義列表適配器。我花了一輩子才弄清楚,最後感謝上帝,我有一個體面的處理。

1-如前所述,你不想創建一個新的活動來呼叫readXML()它與你的活動在同一個類中,因此只需調用它即可。

2-將public ArrayList<Station> stationList = new ArrayList<Station>();改爲private List<Station> stationList = new ArrayList<Station>();。不需要公佈它,並且List是比ArrayList()更好的變量。

3-你可以發佈你的'main_activity.xml`文件。

4-你說你的解析器工作,所以你是100%,在調用readXML()變量stationList包含你的dataXML的所有值的權利?

然後讓我知道。

+0

感謝您的理解。我發佈了主要活動xml。並從數組列表更改爲「列表」。當我在單獨的java測試文件中測試時,XML工作正常,但我無法從實際的android活動中進行驗證。 – David

+0

從這兩個'android:layout_below =「@ + id/textView1」'和'android:layout_toRightOf =「@ + id/textView1」'中刪除+,你要添加id三次,這應該會產生一個錯誤,你只能添加一次ID。然後在調用'readXML()':'Log.v(「station name」,stationList.get(0).getStationName()+「」);'後查看它是否在logcat的。 – LuckyMe

+0

感謝您的建議,但它沒有奏效。刪除+的沒有任何影響,並且在readXML()之後放置Log.v等導致致命錯誤。 – David

相關問題