Java变量类名
我正在创建一个带有字符串和类的新对象。假设我有一个名为“Quiz.java”的类。有效方法:
headerItem = new TreeFieldItem(new QuizMenuItem(key, Quiz.class));
我的“value”字符串返回为“Quiz”。我该如何进行这项工作?
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Class.forName(String)
http://mindprod.com/jgloss/classforname .html
您需要确保值字符串是类(包括包)的完全限定名称。
Use
Class.forName(String)
http://mindprod.com/jgloss/classforname.html
You'll need to make sure that the value string is the fully qualified name of the class (including the package).