arr_title,arr_description,arr_img是数组列表
arr_title.add(title);
arr_description.add(description);
arr_img.add(R.drawable.ic_launcher);
arr_img,这里有一个工作正常的静态图像。但是,如何从图像URL动态完成它,以便我从url获取图像并将其传递到此arr_img,而不仅仅是可绘制的图像。
我试过,使用Picaso库,但我不明白,如何使用它代替drawable?
这是我的ArrayAdapter
类MyClass扩展了ArrayAdapter {
public MyClass(Context context, int resource, ArrayList img,ArrayList txt,ArrayList img_res)
{
super(context, resource,img);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inf=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.post_list_design,null);
t1=(TextView)convertView.findViewById(R.id.title_list_design);
t1.setText(arr_title.get(position).toString());
t2=(TextView)convertView.findViewById(R.id.desc_list_design);
t2.setText(arr_description.get(position).toString());
i3=(ImageView)convertView.findViewById(R.id.img_logo);
i3.setImageResource((Integer)arr_img.get(position));
return convertView;
}
}
答案 0 :(得分:2)
试试这个,它在一个String中更好地存储图像URL,并在Load方法中传递字符串,而不是直接传递URL。有用。对我而言,它很有效。
final ImageView image = (ImageView) view.findViewById(R.id.imageView1);
Picasso.with(getApplicationContext()).load("Image Url").into(image);
答案 1 :(得分:1)
Picasso.with(context)
.load(arr_img.get(position))
.resize(100, 100)
.centerCrop()
.into(i3);
试试这个!
答案 2 :(得分:1)
以下是如何做到这一点,让我们假设这是url
列表
static final String BASE = "http://i.imgur.com/";
static final String EXT = ".jpg";
static final String[] URLS = {
BASE + "CqmBjo5" + EXT, BASE + "zkaAooq" + EXT, BASE + "0gqnEaY" + EXT,
BASE + "9gbQ7YR" + EXT, BASE + "aFhEEby" + EXT, BASE + "0E2tgV7" + EXT,
BASE + "P5JLfjk" + EXT, BASE + "nz67a4F" + EXT, BASE + "dFH34N5" + EXT,
BASE + "FI49ftb" + EXT, BASE + "DvpvklR" + EXT, BASE + "DNKnbG8" + EXT,
BASE + "yAdbrLp" + EXT, BASE + "55w5Km7" + EXT, BASE + "NIwNTMR" + EXT,
BASE + "DAl0KB8" + EXT, BASE + "xZLIYFV" + EXT, BASE + "HvTyeh3" + EXT,
BASE + "Ig9oHCM" + EXT, BASE + "7GUv9qa" + EXT, BASE + "i5vXmXp" + EXT,
BASE + "glyvuXg" + EXT, BASE + "u6JF6JZ" + EXT, BASE + "ExwR7ap" + EXT,
BASE + "Q54zMKT" + EXT, BASE + "9t6hLbm" + EXT, BASE + "F8n3Ic6" + EXT,
BASE + "P5ZRSvT" + EXT, BASE + "jbemFzr" + EXT, BASE + "8B7haIK" + EXT,
BASE + "aSeTYQr" + EXT, BASE + "OKvWoTh" + EXT, BASE + "zD3gT4Z" + EXT,
BASE + "z77CaIt" + EXT,
};
并在您的适配器中
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inf=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertView=inf.inflate(R.layout.post_list_design,null);
t1=(TextView)convertView.findViewById(R.id.title_list_design);
t1.setText(arr_title.get(position).toString());
t2=(TextView)convertView.findViewById(R.id.desc_list_design);
t2.setText(arr_description.get(position).toString());
// Get the image URL for the current position.
String url = getItem(position);
i3=(ImageView)convertView.findViewById(R.id.img_logo);
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.fit()
.tag(context)
.into(i3);
return convertView;
}
}
这是非常基本的示例,当然您必须先将图像列表传递给适配器。
希望有所帮助
您可以查看here详细示例
答案 3 :(得分:1)
最好在数组列表中使用getter setter方法。 然后将图像设置为字符串。
并在适配器中将每个URL加载到imageview中,如...
def home(request):
return HttpResponse("Hello, world. You're at the polls index.")