简体   繁体   English

如何转换类型类 <?> 诠释

[英]How to convert type Class<?> to int

I am trying to convert this data type to call out the method later on in another class to switch around layouts being made in other methods such as recipe1Layout(); 我试图将这种数据类型转换为稍后在另一个类中调出该方法,以切换在其他方法(如recipe1Layout();进行的布局recipe1Layout(); by the index number of a class that has a field of a Class<?> Array. 用具有Class<?>数组字段的Class<?>的索引号表示。

Here is the getItem() method 这是getItem()方法

public int getItem(){
     int index = 0;
     for(int i = 0; i < 100; i++) {
         try{
           index = recipe.getClass().getField("Classes").get(i);
         } catch(Exception e){

         }
     }
     return index;
 }

Here is the Recipe Class 这是食谱课

    public class Recipes {
      public Class<?>[] Classes = {
        ChileConLecheActivity.class,
        ArrozActivity.class,
        EnchiladasActivity.class,
        SopaActivity.class
      };
    }

The type of Class needs to be here because I have other uses for the recipe class. 类的类型需要在这里,因为我对配方类还有其他用途。
For example, making a new instance of all classes to later on be called out to make adjustments to all the classes with one method. 例如,调出一个新的所有类的实例,以便以后使用一种方法对所有类进行调整。
The only thing I can think of is converting the type Class to an int so I can call out the method returning the index number I can do something like recipe. 我唯一能想到的就是将Class类型转换为int,这样我就可以调出返回索引号的方法,我可以做一些像配方这样的事情。

    index = Integer.parseInt(Classes[I].getName().toString());

But this is where I am asking for help I have no idea how to get rid of the error in the logcat. 但这是我寻求帮助的地方,我不知道如何摆脱logcat中的错误。

The error shows up as 错误显示为

IndexOutOfArrayException IndexOutOfArrayException

First off, stop using reflection. 首先,停止使用反射。 Use a public static array. 使用公共静态数组。

public class Recipes {
  public static final Class<?>[] CLASSES = {
    ChileConLecheActivity.class,
    ArrozActivity.class,
    EnchiladasActivity.class,
    SopaActivity.class
  };
}

Then, assuming your recipe instance has a field of what Class<Activity> it is assigned to, then, you would want something like this 然后,假设您的recipe实例具有分配给它的Class<Activity>的字段,那么您将需要这样的内容

 public int getItem(){
     int index = -1;
     for(int i = 0 ; i < Recipe.CLASSES.length; i++) {
        if (recipe.getActivityClass().equals(Recipe.CLASSES[i]) { 
           index = i;
           break; 
        } 
     }
     return index;
 }

However, under certain situations, coupling one Activity class to any single Recipe instance, probably isn't a good idea. 但是,在某些情况下,将一个Activity类与任何单个Recipe实例耦合可能不是一个好主意。

I am trying to convert this data type to call out the method later on in another class to switch around layouts being made in other methods 我正在尝试将此数据类型转换为稍后在另一个类中调用该方法以切换在其他方法中进行的布局

if I understand what you are trying to do, you want a some mapping structure to some classes which have some pre-defined layouts. 如果我了解您要执行的操作,则需要一些映射结构到具有某些预定义布局的某些类。

Generally, this can be done with enums and OOP patterns 通常,这可以通过枚举和OOP模式来完成

Have some base classes like this 有一些像这样的基类

public interface Layoutable {
    int getLayout();
}

public enum Recipe {
    ChileConLeche(R.layout.chile_con_leche),
    Arroz(R.layout.arroz),
    Enchiladas(R.layout.enchiladas),
    Sopa(R.layout.sopa)

    int layout;
    Recipe(int layout) { this.layout = layout };
}

Ideally, you would want to use Fragments, but here is an example of an Activity structure 理想情况下,您将要使用片段,但这是一个Activity结构的示例

public abstract class RecipeActvity extends AppCompatActivity implements Layoutable {
    protected Recipe recipe;
    protected int getLayout() { return recipe.layout; }
}

public class ChileConLecheActivity extends RecipeActvity {
    public ChileConLecheActivity() {
        this.recipe = Recipe.ChileConLeche;
    }

    @Override
    public void onCreate(...) {
        setContentView(getLayout());
    }
}

You can also combine this with a Map<Recipe, Class<RecipeActivity>> , from which you would use map.get(Recipe.ChileConCarne) to get the respective class element, for which you can startActivity() with 您还可以将其与Map<Recipe, Class<RecipeActivity>> ,从中您可以使用map.get(Recipe.ChileConCarne)获得各自的类元素,您可以使用以下方法启动startActivity()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM