ListView causing OutOfMemory Error
- by Michael
So I am not really given a reason to the right of this error message. I am not exactly sure why this is happening but my guess though is that it has to do with the fact that there are around ~50 good quality drawables. Upon scrolling really fast, the app crashes. I feel as if I am mitigating most common issues with ListView and crashing such as using View Holders as well as only initiating the inflater once.
Process: com.example.michael.myandroidappactivity, PID: 20103
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
Here is the code
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Integer> imageIds;
private static LayoutInflater inflater;
public ImageAdapter(Context _context, ArrayList<Integer> _imageIds)
{
context = _context;
imageIds = _imageIds;
}
@Override
public int getCount()
{
return imageIds.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder{
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
View rowView = null;
if(rowView==null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.listview_layout, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) rowView.findViewById(R.id.flag);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.img.setImageResource(imageIds.get(position));
return rowView;
}
}