gpt4 book ai didi

java - Android 从 URL 设置 ListView 图片

转载 作者:行者123 更新时间:2023-12-01 18:26:45 25 4
gpt4 key购买 nike

我有一个包含图像的新闻源(ListView)。我想从网络加载图像并在我的 Activity 中使用。所以我在 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;
}
}

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

最佳答案

关于java - Android 从 URL 设置 ListView 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25864320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com