我正在尝试使用ListView
创建自定义ArrayAdapter
。 POJO Item
类:
public class Item {
String itemTitle = "", itemTimestamp = "", itemDescription = "";
ImageView itemImage;
public String getTitle() {
return itemTitle;
}
public String getDescription() {
return itemDescription;
}
public String getTimeStamp() {
return itemTimestamp;
}
public ImageView getImage() {
return itemImage;
}
public Item(String itemTitle, String itemTimestamp, String itemDescription,
ImageView itemImage) {
super();
this.itemTitle = itemTitle;
this.itemDescription = itemDescription;
this.itemTimestamp = itemTimestamp;
this.itemImage = itemImage;
}
public void setItemTitle(String title) {
itemTitle = title;
}
public void setItemTimestamp(String timestamp) {
itemTimestamp = timestamp;
}
public void setItemDescription(String itemDesc) {
itemDescription = itemDesc;
}
public void setItemImage(ImageView image) {
itemImage = image;
}
}
以下是自定义数组适配器的外观。它如下所示:
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_default, null);
}
Item p = items.get(position);
if (p != null) {
TextView list_title = (TextView) v.findViewById(R.id.list_title);
TextView list_description = (TextView) v.findViewById(R.id.list_description);
TextView list_timestamp = (TextView) v
.findViewById(R.id.list_timestamp);
ImageView list_image = (ImageView) v.findViewById(R.id.list_image);
if (list_title != null) {
list_title.setText(p.getTitle());
}
if (list_description != null) {
list_description.setText(p.getDescription());
}
if (list_timestamp != null) {
list_timestamp.setText(p.getTimeStamp());
}
if (list_image != null) {
list_image.setImageResource(p.getImage(R.drawable.btn_bg_pressed));
}
}
return v;
}
}
错误:
The method setImageResource(int) in the type ImageView is not applicable for the arguments (ImageView)
我不确定如何设置ListView
的默认图像,并且应根据我在互联网上运行时输入的数组进行更改。
答案 0 :(得分:3)
您正尝试将ImageView
对象指定为图像(图片)。
在Item
课程而不是ImageView
,您可以将id
(例如R.drawable.some_name
)该特定项目对象的图片存储为int
(如R.drawable.btn_bg_presse
d)然后在您的getView()
方法中,作业将是:
list_image.setImageResource(p.getImageId())
其中方法getImageId()(来自类Item)将是这样的:
private int itemImageId;
public int getImageId() {
return itemImageId;
}