0

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.

1 Answer 1

3

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;
} 
Sign up to request clarification or add additional context in comments.

3 Comments

the image has to change dynamically but show the default image from the local drawable folder till its loaded. could you plz show can i assign this image in the code ?
@HarshaMV i don't think i understand your problem. In your custom row layout R.layout.list_item_default you have an ImageView. Have you tried to set the attribute src to your ImageView like this: android:src="@drawable/some_drawable" so your ImageView already has a picture?
yeah i have done that to get a default image. now when i feed an Array the image view should show an image from a URL mentioned. i have 4 fields - 3 text view and 1 of them is the image view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.