Reputation: 2475
This is my Base Adapter and I am trying to inflate a GridView with Images from drawable. As it turns out, it returns NullPointerExceptipn. But when I change getCount() return value to 0, it does not show exception anymore but the gridview does not show up. I have taken out the log of build_list and it has data in it.
public class BuildingListAdapter extends BaseAdapter {
Context context;
ArrayList<Building> build_list = new ArrayList<Building>();
public BuildingListAdapter(Context c, ArrayList<Building> build_list) {
// TODO Auto-generated constructor stub
context = c;
this.build_list.addAll(build_list);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return build_list.size();
}
@Override
public Building getItem(int position) {
// TODO Auto-generated method stub
return build_list.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.v("CheckifRun", "true");
Building building = this.getItem(position);
ImageView img_view = new ImageView(convertView.getContext());
int resID = convertView.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
Log.v("resourceID", Integer.toString(resID));
img_view.setImageResource(resID);
img_view.setScaleType(ScaleType.CENTER_CROP);
img_view.setLayoutParams(new GridView.LayoutParams(70, 70));
return img_view;
}
}
Upvotes: 0
Views: 386
Reputation: 11948
change getView method to:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.v("CheckifRun", "true");
Building building = this.getItem(position);
ImageView img_view = new ImageView(context);
int resID = context.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
Log.v("resourceID", Integer.toString(resID));
img_view.setImageResource(resID);
img_view.setScaleType(ScaleType.CENTER_CROP);
img_view.setLayoutParams(new GridView.LayoutParams(70, 70));
return img_view;
}
you need your context for creating ImageView
and get data from Resource
what have Changed ?
ImageView img_view = new ImageView(context);
and
int resID = context.getResources().getIdentifier(building.getBuild_img(), "drawable", "com.vapp.yangonuniversity");
Update
you got OutOfMemomryException
,
Why?
OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap
How to fix this issue?
you can search about that in google to find many related question, for start you can see:
Strange out of memory issue while loading an image to a Bitmap object
Upvotes: 1