从资产填充 GridView 时出现问题
我试图用膨胀的视图填充 Android GridView,视图有 ImageView 和 TextView,它们是从数据的 ArrayList 填充的。
一切都很好,但是当我滚动网格时,我的前 7 项正在重复。
namCont.setAdapter(new ImageAdapter(getApplicationContext()));
我的代码:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListView 中的视图会被回收。所以最终,我想当你到达位置 8 时,它会回收它的第一个视图,并且在你的代码块
view = ConvertView;
中你所做的就是返回现有的回收视图。相反,你需要这样做。
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.