Reputation: 11146
I am writing an application where requirement is to dimply drawable in two column sequence. This is my Adapter.getView(..) implementation:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if (convertView == null) { // if it's not recycled, initialize some attributes
View view = inflater.inflate(R.layout.grid_item, parent);
imageView = (ImageView) view.findViewById(R.id.picture);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(drawable[position]);
return imageView;
}
But while to convert it ,,it responding with following error log:
E/AndroidRuntime(10419): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
E/AndroidRuntime(10419): at android.widget.GridView.onMeasure(GridView.java:1056)
E/AndroidRuntime(10419): at android.view.View.measure(View.java:15356)
Any suggestion what I am missing here?
Upvotes: 0
Views: 297
Reputation: 1
It seems like your view has been attached to parent, try this:
view = inflater.inflate(R.layout.grid_item, parent, false);
and
return view;
Upvotes: 0
Reputation: 18923
Simply change this.
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = inflater.inflate(R.layout.grid_item, parent);
imageView = (ImageView) convertView.findViewById(R.id.picture);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(drawable[position]);
return convertView;
OR
View view = convertView ;
if (view == null) { // if it's not recycled, initialize some attributes
view = inflater.inflate(R.layout.grid_item, parent);
imageView = (ImageView) view.findViewById(R.id.picture);
} else {
imageView = (ImageView) view;
}
imageView.setImageResource(drawable[position]);
return view ;
Upvotes: 0
Reputation: 2955
Try like this, add return convertView;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder mHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
mHolder = new ViewHolder();
mHolder.imageView = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
try {
imageView.setImageResource(drawable[position]);
} catch (Exception e) {
// TODO: handle exception
}
return convertView;
}
private class ViewHolder {
private ImageView imageView;
}
Upvotes: 2
Reputation: 4549
you are returning imageView instead of your view which you inflated Write this.
return view ;
Upvotes: 1