从资产填充 GridView 时出现问题

发布于 2024-10-20 14:13:18 字数 1742 浏览 4 评论 0原文

我试图用膨胀的视图填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-10-27 14:13:18

ListView 中的视图会被回收。所以最终,我想当你到达位置 8 时,它会回收它的第一个视图,并且在你的代码块 view = ConvertView; 中你所做的就是返回现有的回收视图。

相反,你需要这样做。

public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.nam_item,
                    null);
        }
        try {
            TextView textView = (TextView) convertView.findViewById(R.id.tekst);
            ImageView imageView = (ImageView) convertView.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();
        }
        return 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.

public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.nam_item,
                    null);
        }
        try {
            TextView textView = (TextView) convertView.findViewById(R.id.tekst);
            ImageView imageView = (ImageView) convertView.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();
        }
        return convertView;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文