question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Casting exceptions when using groups

See original GitHub issue

Hi, I have a fairly complex use case where I have 4 kinds of posts (simple message, photo, video, embedded link). All of this posts share in common a header, a text section that’s where the user writes down a message and a comments section.

I decided it would be better to separate each section into its own model (so I have HeaderModel_, TextContentModel_, PhotoModel_, VideoModel_, EmbeddedLinkModel_ and CommentsModel_). Each model is created using the @ModelView attribute, inflates a layout and has it’s own properties and requirements (the PhotoModel_ for example has a photoUrl property, while the video one has a videoUrl, while the embedded link model has url, previewPhotoUrl, etc).

I was planning on grouping them because all this models need to be put together given the post type and represented inside a CardView, so I have also a class called PostGroup (which extends from EpoxyModelGroup) #where I discuss the kind of post received and given the outcome I build the full post as a group (so, If it’s a photo post I would add to the group HeaderModel_, TextContentModel_, PhotoModel_ and CommentsModel_). For this, my group layout has 4 ViewStubs (I always end up adding 4 models regardless of the post).

This approach sadly seems to fail with a ClassCastException stating that, PhotoModel_ can’t be casted to VideoModel_, or that VideoModel_ can’t be casted to EmbeddedLinkModel_, etc.

Any reasons why this approach might fail? Should I separate each type of post on it’s own group even if mostly all of them display the same amount of Models? Each group being added to the adapter has it’s own Id (I doubled checked this). If not this one, which approach should I follow to be able to do what I stated above? (grouping posts given its type so they can be rendered inside a CardView).

Here’s an shortened example of the group layout I’m using:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
elihartcommented, Jan 31, 2018

I would like to on this and https://github.com/airbnb/epoxy/issues/384 soon

1reaction
elihartcommented, Sep 18, 2017

@mradzinski You might not need a separate group class. In my case I use one group and assign the layout dynamically based on type

class WLVotingWrapperModel extends EpoxyModelGroup {

    WLVotingWrapperModel(WishListItem item, WLVotingRowModel_ votingRowModel, EpoxyModel<?> cardModel) {
        super(0, cardModel, votingRowModel);

        switch (item.getItemType()) {
            case Home:
                layout(R.layout.model_wl_voting_wrapper_home);
                break;
            case Place:
            case PlaceActivity:
                votingRowModel.gridMode(true);
                layout(R.layout.model_wl_voting_wrapper_place);
                break;
            case Trip:
                votingRowModel.gridMode(true);
                layout(R.layout.model_wl_voting_wrapper_trip);
                break;
            default:
                throw new IllegalStateException("Unknown type: " + item.getItemType());
        }
    }
}

You actually can override view type already with the getViewType method. If you want you could just return a unique id there based on your item type and use the same layout file for everything.

The nested recycling would be a bit trickier. I think it would require something like this:

  1. Be able to mark a model within a group as recyclable
  2. Remove recyclable views from the model viewgroup when the group is recycled
  3. Have a custom view pool to recycle those views into
  4. When a group is rebound, for each recyclable model, get a view from the pool or create a new one
  5. insert the recycled view back into the model viewgroup at the correct location (accounting for potential nested view groups

This seems very doable, but definitely some complexity. We only use groups in one place so I haven’t prioritized it. It would be cool though, and I’m open to pull requests!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ClassCastException while using GROUP BY clause in JPA
I am working with JPA. while doing GROUP BY clause example it's throwing ClassCastException. ... My POJO class Fields are: @Column(name = " ......
Read more >
getting class casting exception while fetching result using ...
Hi Experts, We are using jalo class search engine to get result from DB, and using below method, List items =FlexibleSearch.
Read more >
Class Cast Exception - Google Groups
ClassCastException: Cannot cast com.hazelcast.client.impl. ... I am getting this exception when i am trying to connect to hazelcast through JUnit test case ...
Read more >
Invalid cast exception on group by | Infragistics Forums
Hi,. We're getting the following exception when adding adding and saving a row to a grouped by xamgrid. We're using netadvantage ultimate ...
Read more >
Class cast exception on ear creation - Oracle Communities
I have an application which works fine under my development mode. I deployed it in websphere and executed it. It works fine at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found