问题描述
我想显示在列表视图SimpleAdapter德的照片。我包括毕加索在我的项目,但我不知道如何使用毕加索与SimpleAdapter。
I'm trying to display te pictures in a SimpleAdapter listview. I included Picasso in my project but i don't know how to use Picasso with a SimpleAdapter.
List<HashMap<String, String>> featured = new ArrayList<HashMap<String, String>>();
for (int i=0;i<ids.length;i++){
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("productname", names[i]);
hm.put("productprice", prices[i]);
hm.put("productimage", images[i]);
featured.add(hm);
}
String[] from = {"productname", "productprice", "productimage"};
int[] to = {R.id.tv_ProductName, R.id.tv_ProductPrice, R.id.icon};
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, featured, R.layout.listitem_featured, from, to);
HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Featured);
featuredList.setAdapter(adapter);
该产品名称和价格都正确显示。该产品图像不显示在所有。这些图像的URL,例如:
The productname and price are displayed correctly. The product image isn't displayed at all. The images are urls, for example:
的
什么必须做正确显示图像?
What has to be done to display the images correctly?
推荐答案
您不能毕加索传到适配器。你必须创建自己的自定义适配器,它并不像听起来那么令人望而生畏。它甚至可以根据 SimpleAdapter
。像这样的:
you can't "pass Picasso" to the adapter. You have to create your own custom adapter, it's not as daunting as it sounds. It may even be based on the SimpleAdapter
. Like this:
public class MyAdapter extends SimpleAdapter{
public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
if(img == null){
img = (ImageView) v.findViewById(R.id.imageOrders);
v.setTag(img); // <<< THIS LINE !!!!
}
// get the url from the data you passed to the `Map`
String url = ((Map)getItem(position)).get(TAG_IMAGE);
// do Picasso
// maybe you could do that by using many ways to start
Picasso.with(context).load(url)
.resize(imageWidth, imageWidth).into(img);
// return the view
return v;
}
}
BTW 在自定义适配器就可以使用它BY:
BTW on Custom Adapter you can use it BY:
Picasso.with(context).load(yoururl)
.resize(imageWidth, imageWidth).into(holder.imageItem);
然后你可以使用这个类不会对参数图片(但它仍然必须内orderList存在)。
Then you can just use this class without the image on the parameters (but it must still exist inside orderList).
ListView list= (ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter =
new MyAdapter(
getActivity(),
orderList,
R.layout.order_usa_row,
new String[]{TAG_PRICE,TAG_TITLE,TAG_PSTATUS,TAG_PRICESYMBOL},
new int[]{R.id.price,R.id.title,R.id.pstatus,R.id.symbol});
list.setAdapter(adapter);
有关图像大小,我认为你需要重新大小它!如上面的例子 .resize(imageWidth,imageWidth)在
希望这帮助!
For image size I think you need to re-size it !! as in example above .resize(imageWidth, imageWidth)
hope this help!!
这篇关于Android的 - 在simpleApapter列表视图如何diplay图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!