1

I'm creating a new object that takes a string and a class. Let's say I have a class called "Quiz.java". What works:

headerItem = new TreeFieldItem(new QuizMenuItem(key, Quiz.class));

My "value" String returns as "Quiz". How do I make this work?

 private ArrayList<TreeFieldItem> getListItems() {

    ArrayList<TreeFieldItem> arrayList = new ArrayList<TreeFieldItem>(14);

    TreeFieldItem headerItem    = null;

    Map<String, String> map = getMenuItems(R.xml.xml_quiz_menu);

    for (Entry<String, String> entry : map.entrySet()) {

        String key = entry.getKey();
        String value = entry.getValue();

        **headerItem = new TreeFieldItem(new QuizMenuItem(key, value.class));**
        arrayList.add(headerItem);

    }

    return arrayList;

}

1 Answer 1

4

Use Class.forName(String)

http://mindprod.com/jgloss/classforname.html

headerItem = new TreeFieldItem(new QuizMenuItem(key, Class.forName(value));

You'll need to make sure that the value string is the fully qualified name of the class (including the package).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.