我的所有图片都存储在drawable文件夹中。我希望看到包含一些图像的图库视图。
我想要的图像名称是字符串数组。
我可以获取图库视图来显示这些图像。
我发现代码如下,这里的图像是整数。
我可以使用String数组吗?
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context){
this.context = context;
}
public int getCount() {
return pics.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(context);
iv.setImageResource(pics);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
答案 0 :(得分:0)
如果您的图片在您的资源中,这很容易。
public class ImageAdapter extends BaseAdapter {
private Context context;
private Resources resources;
private String[] imageNames;
public ImageAdapter(Context context, String[] imageNames){
this.context = context;
this.resources = context.getResources();
this.imageNames = imageNames;
}
public int getCount(){
return imageNames.length;
}
public String getItem(int position){
return imageNames[position];
}
public long getItemId(int position){
return position;
}
public View getView(ViewGroup parent, View convertView, int position){
if(convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.imageview, parent, false);
}
int imageId = resources.getIdentifier(getItem(position), "drawable", context.getPackageName());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
imageView.setImageResource(imageId);
return convertView;
}
}