Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;

    //List of Station Objects
    public ArrayList<Station> StationList = new ArrayList<Station>();

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);

    //object for method call to read XML document
    MainActivity activity1 = new MainActivity();
    activity1.readXML();

     final ListView listview = (ListView) findViewById(R.id.listView1);

     //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());
        }

     //Array adapter
     final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, nameList);
     listview.setAdapter(adapter);  

}

@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;
}

}
//Class creating Adapter
class StableArrayAdapter extends ArrayAdapter<String> {

HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

public StableArrayAdapter(Context context, int textViewResourceId,
    List<String> objects) {
  super(context, textViewResourceId, objects);
  for (int i = 0; i < objects.size(); ++i) {
    mIdMap.put(objects.get(i), i);
  }
}

public long getItemId(int position) {
  String item = getItem(position);
  return mIdMap.get(item);
}

public boolean hasStableIds() {
  return true;
}

}
share

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.