以下代码确实有效:
ArrayList<String> menuItems = new ArrayList<String>(Arrays.asList(new String[] {
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
}));
ListView listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems);
mAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
listView.setAdapter(mAdapter);
但是当我使用ArrayAdapter<WrapperType<T>>
代替时ArrayAdapter<String>
,它不起作用;Android 将声明应用程序已意外停止。
为什么它没有按预期工作?不能将ArrayAdapter
参数化类型作为参数处理?
PS:想象一下,类型T
只是一个带有几个属性的类。该WrapperType<T>
类型是参数化的,并且有一个toString()
使用T
类属性的方法。
更新:
我读了 LogCat (它隐藏在某个地方),似乎我得到了一个NoClassDefFoundError
on class ContentManager$1
。
我检索ArrayList<WrapperType<T>> menuItems
如下:
ArrayList<WrapperType<T>> menuItems = ContentManager.readIt();
内容管理器类:
public class ContentManager {
public static ArrayList<WrapperType<T>> readIt() {
T t1 = new T("One", "1");
T t2 = new T("Two", "2");
WrapperContainer<T> container = new WrapperContainer<T>(Arrays.asList(new T[] { t1, t2 })) {
protected void initParams(WrapperType<T> dc) {
dc.setParam("%a%", dc.extract().getPropA());
dc.setParam("%b%", dc.extract().getPropB());
}
protected void initFormatting(WrapperType<T> dc) {
String ft = "%a% - %b%";
setFormatting(ft);
}
};
return container.getDisplayWrappers();
}
}
WrapperType 类:
class WrapperType {
protected T element;
private String formatting = "";
protected HashMap<String, String> params = new HashMap<String, String>();
public DisplayWrapper(T element) {
this.element = element;
initParams();
initFormatting();
}
protected void initParams() { }
protected void initFormatting() { };
public final void setParam(String key, String value) {
this.params.put(key, value);
}
public final void setFormatting(String formatting) {
this.formatting = formatting;
}
public final T extract() {
return this.element;
}
private final String getFormattedText() {
String ft = this.formatting;
for (String key : this.params.keySet()) {
ft = ft.replace(key, this.params.get(key));
}
return ft;
}
@Override
public String toString() {
String ft = getFormattedText();
return ((ft != null && !ft.equals("")) ? ft : "[UNFORMATTED_STRING]");
}
}
WrapperContainer 类:
public abstract class WrapperContainer<T> {
private ArrayList<WrapperType<T>> containers = new ArrayList<WrapperType<T>>();
protected HashMap<String, String> params = new HashMap<String, String>();
public WrapperContainer(List<T> elements) {
for (T element : elements) {
WrapperType<T> dw = new WrapperType<T>(element) {
protected void initParams() {
WrapperType<T> dw = this;
WrapperContainer.this.initParams(dw);
}
};
this.containers.add(dw);
}
}
protected abstract void initParams(WrapperType<T> dw);
protected void initFormatting(WrapperType<T> dw) { }
protected void setFormatting(String formatting) {
for (WrapperType<T> dw : this.containers) {
dw.setFormatting(formatting);
}
}
public ArrayList<WrapperType<T>> getDisplayWrappers() {
return this.containers;
}
}