Search code examples
androidandroid-listviewandroid-arrayadapterandroid-imageview

ImageView in ListView with custom ArrayAdapter


I am trying to create a custom ListView using an ArrayAdapter. POJO Item class:

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;
    }

}

And here how the custom array adapter looks like.It is shown as below:

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;
}

}

Error:

The method setImageResource(int) in the type ImageView is not applicable for the arguments (ImageView)

I am not sure how to set the default image for the ListView and that should change according the the array I feed it in at run time from the internet.


Solution

  • You are trying to assign as an image(picture) a ImageView object. In your Item class instead of an ImageView you could store the id(like R.drawable.some_name) of the image for that particular item object as an int(like R.drawable.btn_bg_pressed) and then in your getView() method the assignment will be :

    list_image.setImageResource(p.getImageId())
    

    where the method getImageId()(from class Item) would be like this:

    private int itemImageId;
    
    public int getImageId() {
       return itemImageId;
    }