我使用Google的Master-Detail项目模板创建了一个包含listview的新片段。我现在需要将一个imageView添加到listView项目中,但是我无法找到如何实现这一点的示例。
我试图使用:
addItem(new DummyItem("1", "Home", image.setImageResource(R.drawable.icon)));
但是我不断地说错误说明:
构造函数Content.DummyItem(String,String,void)未定义
P.S。
遵循eclipse的建议添加:
public DummyItem(String id2, String content2, Object setImageResource) {
// TODO Auto-generated constructor stub
}
......只会导致更多错误。 (我只需要快速掌握它 - 这是我第一次使用碎片,所以我只需要快速掌握基础知识。)
public class Content {
static ImageView image;
/**
* An array of sample (dummy) items.
*/
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
static {
// Add 3 sample items.
addItem(new DummyItem("1", "Home", image.setImageResource(R.drawable.icon)));
addItem(new DummyItem("2", "Before & After Photos", image.setImageResource(R.drawable.icon)));
addItem(new DummyItem("3", "Video Testimonials", image.setImageResource(R.drawable.icon)));
addItem(new DummyItem("4", "Audio Testimonials", image.setImageResource(R.drawable.icon)));
addItem(new DummyItem("4", "Written Testimonials", image.setImageResource(R.drawable.icon)));
}
private static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public String id;
public String content;
private Object object;
public DummyItem(String id, String content, Object setImageResource) {
this.id = id;
this.content = content;
this.object = object;
}
public DummyItem(String id2, String content2, Object setImageResource) {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return content;
}
}
}
答案 0 :(得分:0)
setImageResource()
的返回类型为void
,这意味着它不会返回任何内容。您可能应该做的是传递资源ID本身并将其作为int
存储在DummyItem中。然后在列表适配器中,您可以在列表项布局中显示的setImageResource()
上调用ImageView
。
public static class DummyItem {
public String id;
public String content;
public int imageResource;
public DummyItem(String id, String content, int imageResource) {
this.id = id;
this.content = content;
this.imageResource = imageResource;
}
}
// elsewhere...
addItem(new DummyItem("1", "Home", R.drawable.icon));
答案 1 :(得分:0)
您是否为ListView创建了CustomAdapter?
你有DummyItem作为存储你的图像资源的模型,接下来我们用ListAdapter将所有资源放到ListView,here代码
不要忘记在ListView上设置适配器
DummyAdapter adapter = new DummyAdapter(this.getActivity, ITEMS);
dummyList.setAdapter(adapter);