在我的项目中使用自定义适配器和网格视图,当我的网格视图加载许多包含图像的项目时滚动我的活动崩溃并重新加载。我在@Raghunandan的帮助下解决了加载图像时出现的资源问题 我的错误是memmory。因为我认为负载大小的图像。
public class MyAdapter extends ArrayAdapter<StructureCase> {
private LayoutInflater mInflater = null;
public Context context;
public Class distinationActivity = null;
public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
// mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public ImageView gem_img = null;
public TextView gem_name = null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final View v;
final StructureCase item = getItem(position);
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.my_grid_list, null);
//mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.my_grid_list, parent, false);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
viewHolder.gem_img = (ImageView) convertView.findViewById(R.id.imageView_mygrid_list);
viewHolder.gem_name = (TextView) convertView.findViewById(R.id.textView_mygrid_list);
viewHolder.gem_name.setTypeface(G.typeFacePrs);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
int temp = 0;
viewHolder.gem_name.setText(item.g_name);
int id = G.currentActivity.getResources().getIdentifier(item.g_image, "drawable", G.currentActivity.getPackageName());
Drawable drawable = G.currentActivity.getResources().getDrawable(id);
if(drawable != null){
viewHolder.gem_img.setImageDrawable(drawable);
}else{
viewHolder.gem_img.setImageResource(R.drawable.almas);
}
//viewHolder.newsThumb.setImageResource(temp);
viewHolder.gem_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
答案 0 :(得分:0)
在getview method
中设置位图时,在ImageView
中使用此代码。
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 800;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
{
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;`enter code here`
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}