Java 无法在ListView中设置图像
我正在尝试实现Pinterest样式的图像视图,如下所示: 我希望从SD卡中的特定文件夹设置图像。我正在尝试以下代码,但无法从文件夹中获取图像(显示一个空白的白色屏幕) 代码:Java 无法在ListView中设置图像,java,android,image,listview,Java,Android,Image,Listview,我正在尝试实现Pinterest样式的图像视图,如下所示: 我希望从SD卡中的特定文件夹设置图像。我正在尝试以下代码,但无法从文件夹中获取图像(显示一个空白的白色屏幕) 代码: private void loadItems(){ String[] leftItems = new String[]{}; String[] rightItems = new String[]{}; ItemsAdapter mAdapter = null; ArrayList<String> image
private void loadItems(){
String[] leftItems = new String[]{};
String[] rightItems = new String[]{};
ItemsAdapter mAdapter = null;
ArrayList<String> image_items = ItemsAdapter.getImages();
System.out.println("The images are obtained in image_items");
System.out.println("The list of images are" + image_items.toString());
mAdapter = new ItemsAdapter(this,R.layout.item,image_items);
listViewLeft.setAdapter(mAdapter);
listViewRight.setAdapter(mAdapter);
leftViewsHeights = new int[leftItems.length];
rightViewsHeights = new int[rightItems.length];
}
public class ItemsAdapter extends ArrayAdapter<String>{
Context context;
LayoutInflater inflater;
int layoutResourceId;
float imageWidth;
InputStream is;
int item;
public ItemsAdapter(Context context, int layoutResourceId, ArrayList<String> items) {
super(context, layoutResourceId, items);
this.context = context;
this.layoutResourceId = layoutResourceId;
float width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
float margin = (int)convertDpToPixel(10f, (Activity)context);
imageWidth = ((width - (3 * margin)) / 2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout row = (FrameLayout) convertView;
ItemHolder holder;
String item = getItem(position);
if (row == null) {
holder = new ItemHolder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (FrameLayout) inflater.inflate(layoutResourceId, parent, false);
ImageView itemImage = (ImageView)row.findViewById(R.id.item_image);
holder.itemImage = itemImage;
} else {
holder = (ItemHolder) row.getTag();
}
row.setTag(holder);
setImageBit(item, holder.itemImage);
return row;
}
public static class ItemHolder
{
ImageView itemImage;
}
// resize the image proportionately so it fits the entire space
private void setImageBit(String path, ImageView imageView){
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
Bitmap bitmap = BitmapFactory.decodeFile(path);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
int count = 10;
String file_name = null;
for(int j = 1;j<count;j++)
{
file_name = "Image " + i + ".jpg";
}
//ArrayList<String> img_bitmap;
//Bitmap bmp = ImgBitFromFile(path, file_name);
imageView.setImageBitmap(bitmap);
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static ArrayList<String> getImages()
{
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File mainfolder = new File(path);
ArrayList<String> files = new ArrayList<String>();
if(mainfolder.listFiles() == null)
return null;
for(File f : mainfolder.listFiles())
{
if(f.getName().contains(".jpg"))
{
files.add(f.getAbsolutePath());
}
}
return files;
}
ItemsActivity.java:
private void loadItems(){
String[] leftItems = new String[]{};
String[] rightItems = new String[]{};
ItemsAdapter mAdapter = null;
ArrayList<String> image_items = ItemsAdapter.getImages();
System.out.println("The images are obtained in image_items");
System.out.println("The list of images are" + image_items.toString());
mAdapter = new ItemsAdapter(this,R.layout.item,image_items);
listViewLeft.setAdapter(mAdapter);
listViewRight.setAdapter(mAdapter);
leftViewsHeights = new int[leftItems.length];
rightViewsHeights = new int[rightItems.length];
}
public class ItemsAdapter extends ArrayAdapter<String>{
Context context;
LayoutInflater inflater;
int layoutResourceId;
float imageWidth;
InputStream is;
int item;
public ItemsAdapter(Context context, int layoutResourceId, ArrayList<String> items) {
super(context, layoutResourceId, items);
this.context = context;
this.layoutResourceId = layoutResourceId;
float width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
float margin = (int)convertDpToPixel(10f, (Activity)context);
imageWidth = ((width - (3 * margin)) / 2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout row = (FrameLayout) convertView;
ItemHolder holder;
String item = getItem(position);
if (row == null) {
holder = new ItemHolder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (FrameLayout) inflater.inflate(layoutResourceId, parent, false);
ImageView itemImage = (ImageView)row.findViewById(R.id.item_image);
holder.itemImage = itemImage;
} else {
holder = (ItemHolder) row.getTag();
}
row.setTag(holder);
setImageBit(item, holder.itemImage);
return row;
}
public static class ItemHolder
{
ImageView itemImage;
}
// resize the image proportionately so it fits the entire space
private void setImageBit(String path, ImageView imageView){
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
Bitmap bitmap = BitmapFactory.decodeFile(path);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
int count = 10;
String file_name = null;
for(int j = 1;j<count;j++)
{
file_name = "Image " + i + ".jpg";
}
//ArrayList<String> img_bitmap;
//Bitmap bmp = ImgBitFromFile(path, file_name);
imageView.setImageBitmap(bitmap);
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static ArrayList<String> getImages()
{
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File mainfolder = new File(path);
ArrayList<String> files = new ArrayList<String>();
if(mainfolder.listFiles() == null)
return null;
for(File f : mainfolder.listFiles())
{
if(f.getName().contains(".jpg"))
{
files.add(f.getAbsolutePath());
}
}
return files;
}
private void loadItems(){
字符串[]leftItems=新字符串[]{};
String[]rightItems=新字符串[]{};
ItemsAdapter mAdapter=null;
ArrayList image_items=ItemsAdapter.getImages();
System.out.println(“图像是在图像项中获得的”);
System.out.println(“图像列表是”+image\u items.toString());
mAdapter=newitemsadapter(这个,R.layout.item,image\u items);
setAdapter(mAdapter);
setAdapter(mAdapter);
leftViewsHeights=newint[leftItems.length];
RightViewsHights=newint[rightItems.length];
}
ItemsAdapter.java:
private void loadItems(){
String[] leftItems = new String[]{};
String[] rightItems = new String[]{};
ItemsAdapter mAdapter = null;
ArrayList<String> image_items = ItemsAdapter.getImages();
System.out.println("The images are obtained in image_items");
System.out.println("The list of images are" + image_items.toString());
mAdapter = new ItemsAdapter(this,R.layout.item,image_items);
listViewLeft.setAdapter(mAdapter);
listViewRight.setAdapter(mAdapter);
leftViewsHeights = new int[leftItems.length];
rightViewsHeights = new int[rightItems.length];
}
public class ItemsAdapter extends ArrayAdapter<String>{
Context context;
LayoutInflater inflater;
int layoutResourceId;
float imageWidth;
InputStream is;
int item;
public ItemsAdapter(Context context, int layoutResourceId, ArrayList<String> items) {
super(context, layoutResourceId, items);
this.context = context;
this.layoutResourceId = layoutResourceId;
float width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
float margin = (int)convertDpToPixel(10f, (Activity)context);
imageWidth = ((width - (3 * margin)) / 2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout row = (FrameLayout) convertView;
ItemHolder holder;
String item = getItem(position);
if (row == null) {
holder = new ItemHolder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (FrameLayout) inflater.inflate(layoutResourceId, parent, false);
ImageView itemImage = (ImageView)row.findViewById(R.id.item_image);
holder.itemImage = itemImage;
} else {
holder = (ItemHolder) row.getTag();
}
row.setTag(holder);
setImageBit(item, holder.itemImage);
return row;
}
public static class ItemHolder
{
ImageView itemImage;
}
// resize the image proportionately so it fits the entire space
private void setImageBit(String path, ImageView imageView){
path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
Bitmap bitmap = BitmapFactory.decodeFile(path);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
int count = 10;
String file_name = null;
for(int j = 1;j<count;j++)
{
file_name = "Image " + i + ".jpg";
}
//ArrayList<String> img_bitmap;
//Bitmap bmp = ImgBitFromFile(path, file_name);
imageView.setImageBitmap(bitmap);
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static ArrayList<String> getImages()
{
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
File mainfolder = new File(path);
ArrayList<String> files = new ArrayList<String>();
if(mainfolder.listFiles() == null)
return null;
for(File f : mainfolder.listFiles())
{
if(f.getName().contains(".jpg"))
{
files.add(f.getAbsolutePath());
}
}
return files;
}
公共类ItemsAdapter扩展了ArrayAdapter{
语境;
充气机;
国际布局资源;
浮动图像宽度;
输入流为;
国际项目;
公共项适配器(上下文上下文、内部布局资源ID、数组列表项){
超级(上下文、布局资源ID、项目);
this.context=上下文;
this.layoutResourceId=layoutResourceId;
浮动宽度=((活动)上下文).getWindowManager().getDefaultDisplay().getWidth();
浮动边距=(int)convertDpToPixel(10f,(活动)上下文);
图像宽度=((宽度-(3*边距))/2);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
FrameLayout行=(FrameLayout)convertView;
物品持有人;
String item=getItem(位置);
if(行==null){
holder=新的ItemHolder();
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
行=(框架布局)充气器。充气(布局资源ID,父项,false);
ImageView itemImage=(ImageView)row.findViewById(R.id.item\u image);
holder.itemImage=itemImage;
}否则{
holder=(ItemHolder)row.getTag();
}
row.setTag(支架);
setImageBit(项目、持有者、项目图像);
返回行;
}
公共静态类ItemHolder
{
图像查看项目图像;
}
//按比例调整图像大小,使其适合整个空间
私有void setImageBit(字符串路径,ImageView){
path=android.os.Environment.getExternalStorageDirectory()+File.separator+“Pictures”+File.separator+“SanPics2”;
位图位图=BitmapFactory.decodeFile(路径);
float i=((float)imageWidth)/((float)bitmap.getWidth());
float imageHeight=i*(bitmap.getHeight());
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)imageView.getLayoutParams();
params.height=(int)图像高度;
params.width=(int)imageWidth;
imageView.setLayoutParams(参数);
整数计数=10;
字符串文件_name=null;
对于(int j=1;j查看与您需要的相同且易于实现的样式。这看起来像是gridview而不是ListView对于这种Pinterest样式,您必须使用gridview而不是ListView。但是,我已经使用drawables文件夹中图像的列表视图实现了上述样式。我将尝试并让您知道。谢谢。我相信这是需要的ires有两个支持库,而不是默认的支持库。我正在使用Eclipse,它会引发某些错误。我在哪里可以找到这些库?您是否已将其库项目添加到您的项目中?@San 3您提供的链接表示StaggedGridView的库,还有另一个名为StaggedGridViewDemo的文件,它表示一个简单的示例。我已将StaggedGridView库添加到演示项目中。但这似乎无法解决错误。错误是某些未知的类文件,因此我相信我必须导入某些其他库!!对吗?