Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am new to android developing and I encountered a problem which I can't solve. I implemented an ExpandableListView with a custom Adapter. I have 5 groups and each group has 1 child item. Each group element is of data type: org.myapp.ui.MyClass.

I'm now trying to get the MyClass Object of each group element when expanding it. For that I use the setOnGroupExpandListener:

 lv.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
    public void onGroupExpand (int groupPosition){

      MyClass selected = (MyClass)lv.getAdapter().getItem(groupPosition);
      servId = selected.getServiceId();

      // Do more stuff here
}
});

When I now test this code in the emulator, i get the following ClassCastException, but only after expanding some groups in the ExpandableListView a few times. So for a few times it works and then suddenly it doesn't and i get this exception.

java.lang.ClassCastException: java.lang.String cannot be cast to org.myapp.ui.MyClass

in this code line

MyClass selected = (MyClass)lv.getAdapter().getItem(groupPosition);

If anyone has an idea why this happens (but only after expanding a few groups in the ExpandableListView), please let me know.

share|improve this question
    
You're casting a String object to a 'MyClass' object - these types are incompatiable - getItem will get the text (String) from the adapter position. – Mark Keen Jun 25 '15 at 8:57
    
but it works for about 10 times before it crashes, so i expand the groups of the exlistiew for a couple of times and it works for these times. – Patricia Kopenhagen Jun 25 '15 at 9:04
    
do you know another way of getting the object of each group item when expanding it? – Patricia Kopenhagen Jun 25 '15 at 9:26
    
solved it myself with this: MyClass selected = (MyClass) lv.getExpandableListAdapter().getGroup(groupPosition); – Patricia Kopenhagen Jun 25 '15 at 10:07
    
Glad to hear .. Always the most satisfying way to solve a problem :) – Mark Keen Jun 25 '15 at 11:10

Taken from oracle docs, ClassCastException Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

 Object x = new Integer(0);
 System.out.println((String)x);

In your case you are trying to convert a string to a custom class which will never pass because the line lv.getAdapter().getItem(groupPosition) gives String instead of MyClass Object.

share|improve this answer
    
But why does it work for a couple of times and then all of a sudden it crashes? That is what is confusing me. – Patricia Kopenhagen Jun 25 '15 at 9:06
    
It never works..as long as your method returning String – Bhargav Kumar R Jun 25 '15 at 9:09
    
do you know if there is another way to get the object of each group item when expanding it? so what i want is to get the object of the group item which i am expanding. – Patricia Kopenhagen Jun 25 '15 at 9:11
    
Refer the class API once.. – Bhargav Kumar R Jun 25 '15 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.