所以我对如何在我的项目中正确实现ImageLoader感到有点困惑,因为我所参与的大多数项目都涉及创建一个全局可访问的ImageLoader对象并通过Adapter访问它。但是我的课程修改如下:
public class ProductGridInflator extends ArrayAdapter<Product>{
private int[] Imageid;
ArrayList<Product> dbProducts;
Context ctx;
int resource;
ArrayList<Image> urlArr = new ArrayList<Image>();
ArrayList<Image> dbImages = new ArrayList<Image>();
public ProductGridInflator(Context context, int resource,ArrayList<Product> objects, ArrayList<Image> obj) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.dbProducts = objects;
this.ctx = context;
this.resource = resource;
this.dbImages = obj;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(dbProducts.size() == 0){
return 0;
}else{
return dbProducts.size();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View child = convertView;
RecordHolder holder;
LayoutInflater inflater = ((Activity) ctx).getLayoutInflater(); // inflating your xml layout
if (child == null) {
child = inflater.inflate(R.layout.grid_single, parent, false);
holder = new RecordHolder();
holder.productName = (TextView) child.findViewById(R.id.grid_text); // fname is the reference to a textview
holder.image = (ImageView) child.findViewById(R.id.grid_image);
child.setTag(holder);
}else{
holder = (RecordHolder) child.getTag();
}
final Product user = dbProducts.get(position);
holder.productName.setText(user.getNAME());
for(int i=0;i<dbImages.size();i++)
{
if(dbImages.get(i).PID.equals(user.PID))
{
//holder.image is my image holder
//This is where the URL should be accessed and image to be added
String URL = dbImages.get(i).URL;
}
else
continue;
}
return child;
}
那么如何在此代码中实现ImageLoader并访问我指定的URL? :)
答案 0 :(得分:0)
看看毕加索图书馆:https://github.com/square/picasso
它会照顾您需要的一切。它将图像加载到ImageView中,甚至支持缓存。它非常易于使用。