以下代码确实有效:
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
上获得了ContentManager$1
。
我按如下方式检索ArrayList<WrapperType<T>> menuItems
:
ArrayList<WrapperType<T>> menuItems = ContentManager.readIt();
ContentManager类:
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;
}
}
答案 0 :(得分:0)
我猜你试图将适配器的源设置为非字符串arraylist;尝试将menuItems转换为字符串数组或字符串列表,然后再将其设置为适配器