Am an Android newbiew. Am trying to load a bunch of images in my res/Drawable folder into a Gridview via an array adapter. unfortunately my the app crashes each time i try to vi
Problem is that you try to assign String-type data to ImageView
.
The easiest way to fix it would be:
Instead of using simple ArrayAdapter
with string data, use custom adapter like:
class ImgAdapter extends BaseAdapter {
@Override
public int getCount() {
return planets.length;
}
@Override
public Integer getItem(final int position) {
return planets[position];
}
@Override
public long getItemId(final int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_row, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageGrid);
imageView.setImageResource(getItem(position));
return view;
}
}
That is simple it. Last thing is to set new adapter to gridView