我最近一直在研究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>
你为什么不使用'ListActity'?也许问题出现在你的布局'activity_main.xml'中,发布你的xml。 您不需要实例化'MainActivity'来调用'readXML()',只需从'onCreate()'调用它即可。 – rubenlop
我已经发布了我的XML。我切换到ListActivity,我试着从OnCreate()中解析XML。现在它只是崩溃。 – David