我最近一直在研究 Android ListViews,但遇到了一些问题。我看了一些教程,让最基本的 ListViews 工作得很好。但现在我想做一个显示一系列对象的 ListView。我以为我做的一切都是正确的。我的代码没有产生任何错误,但我希望创建的列表根本不显示。显然,我不确定为什么。
此应用程序的目的是编译气象站和机场列表并显示信息(例如名称、ID#、坐标等) - 所有这些都从 XML 文档中解析并包含在 Arraylist 中(由单独的具有适当构造函数/getter/setter 的类)。基本上我得到一个站列表 - 然后从该列表中 - 一个设置为适配器的站名称列表。我的 ListView 应该只显示每个站的名称,但无济于事。
我已经测试了我的解析器和我的数组。一切正常,只是没有显示。根据所有教程,我的逻辑应该正确到我的适配器。有没有人有什么建议?我觉得我正在用尽所有解决方案。我的代码发布在下面:
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>