java - SimpleAdapter 不在 GUI 上显示数据

标签 java android arrays android-arrayadapter simpleadapter

这是我为我工作的大学编写的第一个 Android 应用程序。它基本上是一个瘦客户端,从站点上的 xml 读取数据并将其打印到列表中。不过,我遇到了一个奇怪的问题,需要一些帮助。 EventsListActivity 从文件中读取一个 xml,将其放入一个 HashMap 数组中,对其进行排序并将其放入一个 SimpleAdapter 中。我正在为我的 DirectoryListActivity 使用完全相同的代码,它工作得很好。但是,EventsListActivity 仅显示包含 1 行的空白列表。如果单击该行将显示正确的数据,并且 xml 文件中只有 1 行,所以这不是问题。但是,行中的 TextView 没有显示任何内容。

我已逐步完成代码,并且正在正确读取和存储 xml。 SimpleAdapter 的绑定(bind)似乎有问题。任何提示将不胜感激,如果您看到我可以在我的代码中做一些更好的事情,请随时给我指点。这是所需的信息:

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

自定义列表.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>

最佳答案

也许尝试替换这些行?

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

对于这些行:

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

关于java - SimpleAdapter 不在 GUI 上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417621/

相关文章:

java - 根据另一个下拉列表选择填充下拉列表

c - 关于复制垃圾

Java猜谜游戏循环错误

java - 返回区域的查找算法?

java - 如何将 java JAXB Schema 类导出到 xsd 文件?

java - Android Studio Gradle 项目同步失败

android - 如何在 Flutter 中创建 HTTP 服务器应用程序?

android - Gson 生成的 apk 和 Debug模式的不同行为

c - 动态分配 C 数组的大小不应该出错吗?

javascript - 总结数组的对象