I've been studying Android ListViews recently and I'm having some issues. I looked at a few tutorials and got the most basic ListViews working just fine. But now I would like to make a ListView that displays a series of objects. I thought I was doing everything correct. My code does not produce any errors but the List I wish to create does not display at all. Obviously, I'm not sure why.
The Purpose of this application is to compile a list of weather stations and airports and display information (ex. names, ID#, coordinates, etc) - all of which is parsed from an XML document and contained in an Arraylist(Built by a separate class with proper constructors/getters/setters). Basically I get a list of stations then - then from that list - a list of station names set to an Adapter. My ListView should display only the names of each station but to no avail.
I've tested my Parser and My Arrays. It all works, just nothing displays. According to all the tutorials my logic should be correct down to my Adapter. Does anyone have any suggestions? I feel like I'm exhausting all solutions. My code is posted below:
public class MainActivity extends Activity {
//local variables
String station_id;
String state;
String station_name;
double latitude;
double longitude;
String html_url;
//ArrayListstationList = new ArrayList ();
public ArrayListstationList = new ArrayList ();
private ListView stationName;
private ArrayAdapterarrayAdapter;
//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 ArrayListnameList = new ArrayList ();
for (int i = 0; i < stationList.size(); ++i) {
nameList.add(stationList.get(i).getStationName());
}
arrayAdapter = new ArrayAdapter(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;
}
}
Here is a sample of XML:
NOAA's National Weather Service
http://weather.gov/
http://weather.gov/images/xml_logo.gif
NOAA's National Weather Service
http://weather.gov
08:00 EST
1140
NFNA
FJ
Nausori
-18.05
178.567
http://weather.noaa.gov/weather/current/NFNA.html
http://weather.gov/xml/current_obs/NFNA.rss
http://weather.gov/xml/current_obs/NFNA.xml
KCEW
FL
Crestview, Sikes Airport
30.79
-86.52
http://weather.noaa.gov/weather/current/KCEW.html
http://weather.gov/xml/current_obs/KCEW.rss
http://weather.gov/xml/current_obs/KCEW.xml
Activity main XML:
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" >android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />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" >
.
stackoverflow.comm
No comments:
Post a Comment