user1661396
user1661396

Reputation: 77

Programmatic ImageView android duplicate rows

So I have a function for an android app that is supposed to take any number of pictures and display them 5 pics for each row, for some reason when it goes to the second row, it just repeats the pics for the first row, for example if I have 7 pics numbered 1-7 they will display: 12345 12 here is my function, thanks for any advice.

public void generateImageView(int number, String path){
        //ImageView[] imageViewArray = new ImageView[number];
        int ROW_ITEMS = 5; // 5 images per row
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);
        int limit = number;//limits the number of created imageViews to number
        int rows = limit / ROW_ITEMS;//number of rows
        int leftOver = limit % ROW_ITEMS; //see if we have incomplete rows
        if (leftOver != 0){
            rows += 1;
        }
        int id = 1000;
        int belowId = R.id.send;
        while (rows > 0){
            int realItemsPerRow = ROW_ITEMS;
            if (leftOver != 0 & rows == 1){
                realItemsPerRow = Math.min(ROW_ITEMS,  leftOver);
            }

        for (int i = 0; i < realItemsPerRow; i++){

            Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png");
            ImageView imageViewArray = new ImageView(MainActivity.this);
            imageViewArray.setId(id);
            imageViewArray.setImageBitmap(myBitmap);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            imageViewArray.setPadding(10,10,0,0);
            imageViewArray.setAdjustViewBounds(true);
            imageViewArray.setMaxHeight(80);
            imageViewArray.setMaxWidth(80);
            if (i==0) {
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            } else {
                lp.addRule(RelativeLayout.RIGHT_OF, imageViewArray.getId() -1);
            }
            lp.addRule(RelativeLayout.BELOW, belowId);

            imageViewArray.setLayoutParams(lp);

            rl.addView(imageViewArray);
            id++;
        }
        belowId = id - 1;
        rows--;

Upvotes: 0

Views: 517

Answers (1)

323go
323go

Reputation: 14274

It's because you're just pulling the same images for each position in the row:

Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png");

Before while( rows > 0 ) add:

int j = 0;

Then change the above to:

Bitmap myBitmap = BitmapFactory.decodeFile(path + ( j * ROW_ITEMS + i ) + ".png");

and under rows--; add:

j++;

Upvotes: 1

Related Questions