0

I have a Newsfeed(ListView) containing images. I want to load images from web and use in my activity. So I have this in my OnCreate() of MainActivity:

    String url = "http://m.c.lnkd.licdn.com/mpr/pub/image-Zo8GfnPFe2SPJa2kv-bwRRP7GivkI_DrZ_bw2xHFJLOvI4T0No8wCrLzo95B7jMdzre/suneet-choudhary.jpg";
    Drawable x = LoadImageFromWebOperations(url);
        FeedUser FeedUser_data[] = new FeedUser[]
        {
            new FeedUser(x, "Usename1", "What the hell man."),
            new FeedUser(x, "Username2", "Why not sunny leone."),
        };

        FeedUserAdapter adapter = new FeedUserAdapter(this, 
                R.layout.listview_item_row, FeedUser_data);

        lv = (ListView)findViewById(R.id.list);
        lv.setAdapter(adapter);

    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }

Here's my FeedUser.java

public class FeedUser {
    public Drawable icon;
    public String title;
    public String review;
    public FeedUser(){
        super();
    }

    public FeedUser(Drawable x, String title, String review) {
        super();
        this.icon = x;
        this.title = title;
        this.review = review;
    }
}

And below is the FeedUser Adapter:

public class FeedUserAdapter extends ArrayAdapter<FeedUser>{

    Context context; 
    int layoutResourceId;    
    FeedUser data[] = null;

    public FeedUserAdapter(Context context, int layoutResourceId, FeedUser[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        FeedUserHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new FeedUserHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.UserName);
            holder.txtReview = (TextView)row.findViewById(R.id.ReviewText);

            row.setTag(holder);
        }
        else
        {
            holder = (FeedUserHolder)row.getTag();
        }

        FeedUser FeedUser = data[position];
        holder.txtTitle.setText(FeedUser.title);
        holder.imgIcon.setImageDrawable(FeedUser.icon);
        holder.txtReview.setText(FeedUser.review);

        return row;
    }

    static class FeedUserHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtReview;
    }
}

Now it doesn't show any error, but the image doesn't load either. I'm a beginner so not sure what could be wrong :/

suneet
  • 400
  • 1
  • 5
  • 19
  • is your loadimagefromweboperations actually returning a drawable or are they null – zoruc Sep 16 '14 at 08:54
  • I followed [this question.](http://stackoverflow.com/questions/6407324/how-to-get-image-from-url-in-android) But seems like I should be using a library instead. – suneet Sep 16 '14 at 09:04
  • 1
    you can save the urls and use picasso's library to set the images into the imageviews https://github.com/square/picasso its very useful – zoruc Sep 16 '14 at 09:06

3 Answers3

2

Depending on your requirements, you can use Universal Image Loader library(https://github.com/nostra13/Android-Universal-Image-Loader) or Picasso library(http://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/).

or go through following link:-

Lazy load of images in ListView

Community
  • 1
  • 1
Pradeep
  • 104
  • 4
  • Sorry should have mentioned before, but i am using [CircularImageView](https://github.com/lopspower/CircularImageView)... Does that change anything? – suneet Sep 16 '14 at 09:27
  • There should not be any problem because CircularImageView is subclass of ImageView but I am not sure. You should try this. – Pradeep Sep 16 '14 at 09:47
1

You could try using the following library, easy to use and still being maintained with new features and bug solves:

https://github.com/nostra13/Android-Universal-Image-Loader

1

If you download http://github.com/square/picasso and use this code it should work for you and all the image downloading work is done by picasso

String url = "http://m.c.lnkd.licdn.com/mpr/pub/image-Zo8GfnPFe2SPJa2kv-bwRRP7GivkI_DrZ_bw2xHFJLOvI4T0No8wCrLzo95B7jMdzre/suneet-choudhary.jpg";
    FeedUser FeedUser_data[] = new FeedUser[] {
        new FeedUser(url, "Usename1", "What the hell man."),
        new FeedUser(url, "Username2", "Why not sunny leone."),
    };

    FeedUserAdapter adapter = new FeedUserAdapter(this, 
            R.layout.listview_item_row, FeedUser_data);

    lv = (ListView)findViewById(R.id.list);
    lv.setAdapter(adapter);

}

public class FeedUser {
     public String iconUrl;
     public String title;
     public String review;
} 

public FeedUser(String url, String title, String review) {
    this.iconUrl = url;
    this.title = title;
    this.review = review;
}

}

public class FeedUserAdapter extends ArrayAdapter{

Context context; 
int layoutResourceId;    
FeedUser data[] = null;

public FeedUserAdapter(Context context, int layoutResourceId, FeedUser[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    FeedUserHolder holder = null;

    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new FeedUserHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.UserName);
        holder.txtReview = (TextView)row.findViewById(R.id.ReviewText);

        row.setTag(holder);
    }
    else {
        holder = (FeedUserHolder)row.getTag();
    }

    FeedUser FeedUser = data[position];
    holder.txtTitle.setText(FeedUser.title);

    Picasso.with(context).load(FeedUser.iconUrl).fit().noFade().into(holder.imgIcon);
    holder.txtReview.setText(FeedUser.review);

    return row;
}

static class FeedUserHolder {
    ImageView imgIcon;
    TextView txtTitle;
    TextView txtReview;
}
zoruc
  • 819
  • 7
  • 13