3

I have a ListView and it should have four different types of items. I searched for it, found different solutions that this is possible and tried to do my stuff just like the other persons did. I've created different ViewHolders (one for each type). When the ListView appears for the first time everything is fine. But when I begin to scroll I get a classCastException. The reason is pretty obvious:

holder = (FeedViewHolder) convertView.getTag();

My adapter tries to get the ViewHolder by getting the tag from the convertView if it's not null. But this ViewHolder seems to be the ViewHolder of the previous item so it can't be casted.

I search StackOverflow and Google but all the examples seem to work as they use the same or pretty equal ViewHolders for their different items. But my Objects really differ from each other and need different ViewHolders. Can anyone help me to solve this?

joschplusa
  • 1,353
  • 1
  • 13
  • 20

2 Answers2

8

Ok a collegue just helped me: I hadn't implemented the method getViewTypeCount. That was it. The Android system didn't knew that there different view types because I hadn't defined them. Thanks anyway to dmaxi!

joschplusa
  • 1,353
  • 1
  • 13
  • 20
1

Define item view types for the ListView and cast the ViewHolder based on the actual view type as the code shows in this answer: Creating ViewHolders for ListViews with different item layouts

Community
  • 1
  • 1
dmaxi
  • 3,267
  • 1
  • 18
  • 15
  • Did it exactly this way and it's crashing. As I already said, it's crashing because the Adapter is trying to cast the ViewHolder of Type A to the ViewHolder of Type B which is simply not possible as they differ from eachother. I don't see where the approach from previous answers to this topic handles this issue. – joschplusa May 14 '13 at 08:13