2013-04-17 27 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