ClassCastException: java.util.HashMap with android SimpleExpandableListAdapter Code Answer

I’m trying to create a simple RSS feed reader application for my mobile computing class. I’ve created a class to manage and hold RSS data.

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

My problem is displaying it, I’m trying to use a ExpandableListView and I keep getting an error when I try to expand an item in the 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)

I don’t understand why I’m getting this error, since my code for making group and child List maps are nearly the same.

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

To be clear, it will display the group list, but won’t expand to show the child list.

For the benefit of future googlers, I’ve been using the following tutorials:

Expandable ListView In Android

Expandable lists

Answer

Looking at the code for SimpleExpandableListAdapter (which I have never used, so I could be wrong here), the groups field is declared as

List<? extends Map<String, ?>> 

while the children should be

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

Which is to say, the children should be a list of lists of maps, not a list of maps as you seem to be providing.

I’d suggest getting rid of the lines where you suppress warnings about generic types, and actually fix the warnings rather than just hiding them. They do prevent this kind of failure, and there are very few situations where they are actually unavoidable.

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji