android - 从 Assets 填充 GridView 时出现问题

标签 android gridview bufferedinputstream

我正在尝试用膨胀 View 填充 Android GridView, View 有 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;
    }

最佳答案

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

相反,您需要这样做。

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;
    }

关于android - 从 Assets 填充 GridView 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5204094/

相关文章:

c# - GridView 删除不起作用

BufferedInputStream 的 Java 可用字节不起作用

java - BufferedInputStream 说它不可用,但仍然可以工作

php - 如何在我的 android 应用程序上打印 PHP echo?

java - Firebase 没有 setter/getter

Android - 修改 XML 中的布局值

android - MonoDroid - 从colors.xml设置背景颜色抛出InvalidOperationException

android - 在后台使用而不是在 Activity/Fragment 中使用时如何在 RxJava2 中处理单个观察者

带有适配器回收单元的android gridview header 解决方案

asp.net - HttpUtility.UrlEncode() 和 Server.UrlEncode() 未对 asp.net 4.0 中的 URL 进行编码