Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda