我正在创建一个简单的应用程序,以便在Android的网格视图中共享图像。我创建了一个数组但是当我尝试创建这些图像的gridview时,我的代码出错了,我无法解决。
这是我的代码:
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context arg2)
{
Object localObject;
this.context = localObject;
}
public int getCount()
{
return MainActivity.this.mThumbIds.length;
}
public Object getItem(int paramInt)
{
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt)
{
return paramInt;
}
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
{
if (paramView != null)
{
MainActivity.this.imageView = ((ImageView)paramView);
}
else
{
MainActivity.this.imageView = new ImageView(this.context);
MainActivity.this.imageView.setLayoutParams(new AbsListView.LayoutParams(90, 90));
MainActivity.this.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
MainActivity.this.imageView.setPadding(5, 5, 5, 5);
}
MainActivity.this.imageView.setImageResource(MainActivity.this.mThumbIds[paramInt].intValue());
return MainActivity.this.imageView;
}
}
}
我在这里收到错误:Type mismatch error: Cannot convert Object to Content
错误指向localObject
答案 0 :(得分:0)
你没有使用arg2吗?
无论如何,尝试实际上对变量进行类型转换:
this.context = (Context) localObject;
答案 1 :(得分:0)
Object localObject;
Context context;
public ImageAdapter(Context arg2)
{
this.context = arg2;
}
而不是MainActivity.this.imageView
使用imageView
(这不是错误)
在必要时也使用@Override
。
答案 2 :(得分:0)
您将像这样使用您的基本适配器并更新您的网格视图,如下面的代码..
public class GridViewImageAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> filePaths = new ArrayList<String>();
private int imageWidth;
public GridViewImageAdapter(Activity activity,
ArrayList<String> imagePaths, int imageWidth) {
this.activity = activity;
this.filePaths = imagePaths;
this.imageWidth = imageWidth;
}
@Override
public int getCount() {
return this.filePaths.size();
}
@Override
public Object getItem(int position) {
return this.filePaths.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(activity);
} else {
imageView = (ImageView) convertView;
}
// get screen dimensions
Bitmap image = decodeFile(filePaths.get(position), imageWidth,
imageWidth);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
imageWidth));
imageView.setImageBitmap(image);
return imageView;
}
public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
try {
File f = new File(filePath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = WIDTH;
final int REQUIRED_HIGHT = HIGHT;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
&& o.outHeight / scale / 2 >= REQUIRED_HIGHT) {
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
将适配器值设置为GridView,Imagepaths我已将我的图像存储在SD卡中,所以只有我给适配器的路径...一切顺利
gridView = (GridView) findViewById(R.id.gridView1);
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths,columnWidth);
// setting grid view adapter
gridView.setAdapter(adapter);