0

I have problem with my GridView. I used it for displaying list of images and adding new to the list. When I start with empty list, everything works perfectly, but if i load images from cache folder, everything stays the same except that GridView has 0 size. What I did wrong?

My custom GridView class:

public class myGridView  extends GridView {

    public myGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public myGridView(Context context) {
        super(context);
    }

    public myGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

My GridAdapter:

private class GridAdapter extends BaseAdapter {

        private ArrayList<Bitmap> sources = new ArrayList<>();
        private View addButton;

        public GridAdapter() {
            addButton = createImage(null);
            ((ShapeableImageView) addButton).setImageResource(R.drawable.add_image);

            addButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!checkStoragePermission()) {
                        requestStoragePermission();
                    } else {
                        pickFromGallery();
                    }
                }
            });
        }

        public void addItem(Bitmap bitmap) {
            sources.add(bitmap);
            GridAdapter.this.notifyDataSetChanged();
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            grid.setLayoutParams(new FrameLayout.LayoutParams(grid.getWidth(), (int) (addButton.getHeight() * (getCount() * 0.5 + 1))));
        }

        @Override
        public int getCount() {
            return sources.size() + 1;
        }

        @Override
        public Bitmap getItem(int i) {
            return sources.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null) {
                view = createImage(null);
            }
            if(i>0)  {
                ((ShapeableImageView)view).setImageBitmap(sources.get(i-1));
            }
            else {
                ((ShapeableImageView)view).setImageResource(R.drawable.add_image);
            }

            return view;
        }
    }

How I load images and put them in adapter:

if (loaded != null) //list of all file names
            if (loaded.size() != 0) {
                for (String s : loaded) {
                    Uri fp = Uri.fromFile(new File(getApplicationContext().getCacheDir(), s));
                    try {
                        FileInputStream stream = new FileInputStream(new File(fp.getPath()));
                        Bitmap bitmap = BitmapFactory.decodeStream(stream);
                        adapter.addItem(bitmap);
                        portfolio.add(s);
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

I spent a lot of time checking debugger but all I found was that GridView simply doesn't expand (in its LayoutParams) when I load from cache.

Edit: I solved issue by adding small delay between creating and loading using Handler.postDelayed()

0 Answers0