I am trying to populate Android GridView with inflated view, view has ImageView and TextView which is populate from ArrayList of data.
Fverything is fine but, my first 7 items are repeating, as I scroll the grid.
namCont.setAdapter(new ImageAdapter(getApplicationContext()));
My code:
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return kat.namirnice.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view;
ImageView imageView = null;
if (convertView == null)
{
view = LayoutInflater.from(mContext).inflate(R.layout.nam_item,null);
try
{
TextView textView = (TextView)view.findViewById(R.id.tekst);
imageView = (ImageView)view.findViewById(R.id.slika);
textView.setText(kat.namirnice.get(position).naziv);
Log.i(TAG, "\n position: " + position);
buf = new BufferedInputStream((assetManager.open("images/" + activKat_int + "/" + position + ".png")));
Bitmap bitmap = BitmapFactory.decodeStream(buf);
Drawable d = new BitmapDrawable(bitmap);
imageView.setImageDrawable(d);
buf.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
view = convertView;
}
return view;
}
The Views in a ListView are recycled. So eventually, I guess when you get to position 8 it goes to recycle its first view, and in your code the block
view = convertView;
all you are doing is returning the existing recycled view.Instead you need to do this.