我最近一直在研究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