tags:

views:

40

answers:

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;

}
+4  A: 

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).

StriplingWarrior
Ah, yes I was just about to edit my answer to include that.
Mark Peters