English 中文(简体)
分类CastException: java.util。 2. 与甲型六氯环己烷和甲型六氯环己烷的对比 简单可扩展 名单A
原标题:ClassCastException: java.util.HashMap with android SimpleExpandableListAdapter

I m试图为我的移动计算班制作简单的RSS馈电读物。 I ve创建了一个管理和维护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);

我的问题正在显示,我试图使用一种可扩展的List言语,在我试图扩大名单上的一个项目时,我就留下了一个错误。

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

很显然,它将展示团体名单,但获得扩大以显示儿童名单。

为帮助未来的神职人员,我正在使用以下辅导:

扩大名单

rel=“nofollow”>Expandable list

最佳回答

看看简单可扩展的ListAdapter(我从未使用过,因此我在这里可能错过)法,这些团体被宣布为实地。

List<? extends Map<String, ?>> 

而儿童应当

List<? extends List<? extends Map<String, ?>>>

也就是说,儿童应当成为地图清单,而不是像你似乎提供的地图清单。

我建议删除你压制一般类型警告的线索,实际上确定警告,而不是躲藏。 它们确实防止了这种失败,几乎没有实际不可避免的情况。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签