我正在尝试为我的移动计算类创建一个简单的 RSS 提要阅读器应用程序。我创建了一个类来管理和保存 RSS 数据。
@SuppressWarnings("unchecked")
SimpleExpandableListAdapter rssItemList = new SimpleExpandableListAdapter(
this, // context (this class)
createTitleList(), // Creating group List
R.layout.parent_row, // Group item layout XML
new String[]{"titles"}, // the key of group item
new int[]{R.id.itemTitle}, // ID of each group item.-Data under the key goes into this TextView
createChildList(), // childData describes second-level entries
R.layout.child_row, // Layout for sub-level entries(second level)
new String[]{"desc"}, // Keys in childData maps to display
new int[]{R.id.itemDesc} // Data under the keys above go into these TextViews
);
setListAdapter(rssItemList);
我的问题是显示它,我正在尝试使用 ExpandableListView,当我尝试展开列表中的项目时,我不断收到错误消息。
E/AndroidRuntime( 2542): FATAL EXCEPTION: main
E/AndroidRuntime( 2542): java.lang.ClassCastException: java.util.HashMap
E/AndroidRuntime( 2542): at android.widget.SimpleExpandableListAdapter.getChildrenCount(SimpleExpandableListAdapter.java:255)
E/AndroidRuntime( 2542): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:561)
E/AndroidRuntime( 2542): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:682)
E/AndroidRuntime( 2542): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:567)
E/AndroidRuntime( 2542): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
E/AndroidRuntime( 2542): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
E/AndroidRuntime( 2542): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 2542): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2542): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2542): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2542): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2542): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2542): at dalvik.system.NativeStart.main(Native Method)
我不明白为什么会出现此错误,因为我制作组和子列表映射的代码几乎相同。
@SuppressWarnings({ "unchecked", "rawtypes" })
private List createTitleList(){
ArrayList itemTitles = new ArrayList();
ArrayList<ItemData> entriesList = (ArrayList<ItemData>) feed.getMessages();
for(int i = 0; i < entriesList.size(); i++){
HashMap m = new HashMap();
m.put("titles", entriesList.get(i).getTitle());
itemTitles.add(m);
}
return itemTitles;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private List createChildList(){
ArrayList itemDesc = new ArrayList(); // item description list
ArrayList<ItemData> entriesList = (ArrayList<ItemData>) feed.getMessages();
for(int i = 0; i < entriesList.size(); i++){
HashMap<String, String> m = new HashMap<String, String>();
m.put("desc", entriesList.get(i).getDescription());
itemDesc.add(m);
}
return (List)itemDesc;
}
需要明确的是,它将显示组列表,但不会展开以显示子列表。
为了未来的谷歌人的利益,我一直在使用以下教程: