从URL设置Android List ListView图像

时间:2014-09-16 08:45:29

标签: java android listview

我有一个包含图片的新闻源(ListView)。我想从网上加载图片并在我的活动中使用。所以我在MainActivity的OnCreate()中有这个:

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

这是我的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;
    }
}

以下是FeedUser适配器:

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

现在它没有显示任何错误,但图像也没有加载。我是初学者,所以不确定会出现什么问题:/

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

您可以尝试使用以下库,易于使用,并且仍在使用新功能和错误解决方案进行维护:

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

答案 2 :(得分:1)

如果您下载http://github.com/square/picasso并使用此代码,它应该适合您,所有图像下载工作都由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;
}

}

公共类FeedUserAdapter扩展了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;
}