Search code examples
javaandroidarraysandroid-arrayadaptersimpleadapter

SimpleAdapter not showing data on GUI


This is the first android app I'm writing for the college I work at. It's basically a thin client that reads data from an xml on the site and prints it out to a List. I've ran into a strange problem though and could use some help. The EventsListActivity reads an xml from file, places it in an Array of HashMaps, sorts it and places it into a SimpleAdapter. I'm using this exact same code for my DirectoryListActivity and it works just fine. However the EventsListActivity only displays a blank list with 1 row. That row if clicked will display the correct data, and there is only 1 row in the xml file so that's not an issue. However the TextView in the row isn't displaying anything.

I have stepped through the code and the xml is being read and stored correctly. There just seems to be a problem with the binding of the SimpleAdapter. Any tips would be greatly appreciated and feel free to give me pointers if you see some better things I could be doing in my code. Here is the needed information:

public class KCCEventsListActivity extends ListActivity {
    public static final ArrayList<HashMap<String, String>> eventsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.customlist);
        SimpleAdapter adapter = new SimpleAdapter(KCCEventsListActivity.this, eventsList, R.layout.customrow, new String[] {"summary", "date"}, new int[] {R.id.tvHeading, R.id.tvSubheading});

        if (eventsList.isEmpty()) {
            getEvents();
        }

        KCCEventsListActivity.this.setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView lv, View v, int position, long id) {
        super.onListItemClick(lv, v, position, id);

        HashMap<String, String> event = eventsList.get(position);
        Toast.makeText(KCCEventsListActivity.this, event.get("date"), Toast.LENGTH_LONG).show();
        //Call AddtoCalendar Intent
    }

    protected void getEvents() {
        try {
            URL url = new URL(getString(R.string.KccEventsAddress));
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("EventItem");

            for (int i=0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                Element element = (Element)node;

                HashMap<String, String> entry = new HashMap<String, String>();

                NodeList titleList = element.getElementsByTagName("Summary");
                Element titleElement = (Element)titleList.item(0);
                titleList = titleElement.getChildNodes();
                entry.put("summary", titleList.item(0).getNodeValue());

                NodeList dateList = element.getElementsByTagName("StartTime");
                Element dateElement = (Element)dateList.item(0);
                dateList = dateElement.getChildNodes();
                entry.put("date", dateList.item(0).getNodeValue());

                NodeList orderList = element.getElementsByTagName("Seconds");
                Element orderElement = (Element)orderList.item(0);
                orderList = orderElement.getChildNodes();
                entry.put("order", orderList.item(0).getNodeValue());

                eventsList.add(entry);
            }

            Comparator<HashMap<String, String>> comparator = new Comparator<HashMap<String, String>>() {

                public int compare(HashMap<String, String> object1, HashMap<String, String> object2) 
                {       
                        return object1.get("order").compareToIgnoreCase(object2.get("order"));
                }
            };      
            Collections.sort(eventsList, comparator);

        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

CUSTOMLIST.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView android:id="@id/android:list"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"></ListView>
  <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="No data" />
</LinearLayout>

CUSTOMROW.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/tvHeading" android:layout_width="wrap_content" android:layout_height="26dip" android:layout_alignParentTop="true" android:textSize="20sp" />
<TextView android:id="@+id/tvSubheading" android:layout_width="wrap_content" android:layout_height="22dip" android:layout_below="@id/tvHeading" android:textSize="16sp" />
</RelativeLayout>

Solution

  • After going over the code several times I finally rewrote the web service to print out a different style of xml. This made the code work perfectly. For some reason the previously used xml file was causing the display to not work properly. I wish I had more info on why this happened. Thanks for the answers.