Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
Using reflection and polymorphism with ViewHolder

I have a json with class name, something like this:

{
   "view_class" : "com.view.MyClass"
}

And I have this hierarchy:

    public class Config {
         public String viewClass;
         ...
    }

    public interface ViewHolderInterface {
         void bindValue(...);
    }

    public class SuperMyClass extends RecyclerView.ViewHolder 
implements ViewHolderInterface {
       ... 
    }

    public class MyClass extends SuperMyClass {
       ... 
    }

I read a json file and I generate a "Config" object, so the idea is this:

public void bindViewHolder(SuperMyClass holder, Config config) {

    Class viewHolderClass;

    try {
        //row.viewHolderClass = "com.view.MyClass"
        viewHolderClass = Class.forName(config.viewClass);

    } catch (final Exception exception) {

    }
    // Here I need to "cast" a holder to MyClass and execute the method bindValue.
    final ViewHolderInterface viewHolderInstance =
        (ViewHolderInterface) viewHolderClass.cast(holder);
    viewHolderInstance.bindObjectValue(...);
}

I have tried to do this cast using reflection but I this throws an exception:

java.lang.ClassCastException: SuperMyClass cannot be cast to MyClass

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

Any idea?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think that my error is using reflection and the same time try to up-cast, but I have not found another way to do this.

Your reflection is ok, but the downcasting is not. Take a look at this question: explicit casting from super class to subclass

Consider that there may be more than one "view_class" type (remember that this class is an implementation of a ViewHolder).

You can inspect the object instance in runtime to find out its class. You need not tell the class by outside means.

You can try a safer approach like:

// mind the comparison order from subclass upward
if (MyClass.class.isInstance(holder)) {
    MyClass myClassHolder = (MyClass) holder;
} else if (SuperMyClass.class.isInstance(holder)) {
    SuperMyClass superMyClassHolder = (SuperMyClass) holder;
} else if (ViewHolder.class.isInstance(holder)) {
    ViewHolder viewHolder = (ViewHolder) holder;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda