2

I'm new to android development and I'm facing a small difficulty regarding the GridView. I have a Gridview in my first class called Game and I added a BaseAdapter called GameAdapter. The code is working fine but when adding the views to the GridView, it doesn't show the first element (at position 0).

public View getView(int position, View convertView, ViewGroup parent) {

    Button b;
    if (convertView == null) {
        b = new Button(mContext);
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
        //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);
    } else {
        b = (Button) convertView;
    }
    Drawable d = new BitmapDrawable(Game.splittedBitmaps[position]);
    b.setBackground(d);
    b.setId(position);
    b.setTag("Image_" + position);
    b.setOnClickListener(Game.s);
    Game.buttons[position] = b;
    return b;
}

Assume: mContext is the Context of the Game class, cols and raw are static integers in the Game class representing number go columns and rows respectively, gv is the actual GridView in Game, Game.splittedBitmaps[] is an array containing the Bitmaps to be set as backgrounds.

So this is my getView() code. Note that when I have the line code:

b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));

This will be the result:

enter image description here

http://postimg.org/image/794mrqpnn/

Note the missing view at position 0.

In my other run, after I removed the line code:

b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));

the picture was shown but the height got shrank.

enter image description here

http://postimg.org/image/nmomaw5sz/

When I added the line code:

b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);

the result became half the way between result 1 and result 2:

enter image description here

postimg.org/image/ubv5qwr4z/

I just want to get the picture at position 0 with the same dimensions of the others.

Edited:

XML of the grid view I'm using:

<GridView
    android:id="@+id/gvGame"
    android:layout_width="fill_parent"
    android:layout_height="375dp"
    android:gravity="center"
    android:horizontalSpacing="3dp"
    android:padding="3dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" />

And this the code regarding the grid view in the Game class:

gv = (GridView) findViewById(R.id.gvGame);
gv.setNumColumns(cols);
gv.setAdapter(new GameAdapter(getApplicationContext()));
LaurentY
  • 7,495
  • 3
  • 37
  • 55
user3396919
  • 141
  • 1
  • 10
  • Have you put a logging (print) statement in your code to MAKE CERTAIN that the 0 case is being treated in all cases? Also, what does your GridView setup code look like? You might also try using a more simple `LayoutParams.FILL_PARENT` so that you don't necessarily have to define size yourself, You should be defining the width and height of a row and column upon creation of the GridView, so a fill parent param `SHOULD` work in this case. – trumpetlicks Mar 30 '14 at 21:54
  • [trumpetlicks](http://stackoverflow.com/users/1408212/trumpetlicks) .. actually i don't have a constant width and height for the views as i cut the picture according to an scanned number of the columns and rows. so the width of each depends on the width of the grid view / number of columns and same goes for the height which depends on the grid view height / number of rows. – user3396919 Mar 31 '14 at 11:24

3 Answers3

0

Im pretty sure you want your code to look as follows, whether this truly answers your question or not.

public View getView(int position, View convertView, ViewGroup parent) {
    Button b;
    if (convertView == null) {
        b = new Button(mContext);
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
        //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);

        // This code from below SHOULDNT need to be done if the view already exists
        // Im also assuming that the drawable WILL NOT change, if it does, the 2
        // lines below may move outside this if statement
        Drawable d = new BitmapDrawable(Game.splittedBitmaps[position]);
        b.setBackground(d);


        b.setId(position);
        b.setTag("Image_" + position);
        b.setOnClickListener(Game.s);
        Game.buttons[position] = b;
    } else {
        b = (Button) convertView;
    }

    return b;
}
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • Yeah I think you're right, this was a small mistake. I fixed it but still the problem is the same. Again, I like to remind you that number of columns and rows are dynamically changing (not constant), otherwise, I would have defined them with the GridView in the xml file. I edited the question (added the grid view code in xml and the way I instantiated it in the Game class). Thanks – user3396919 Mar 31 '14 at 15:43
  • @user3396919 - I don't mean defining them statically in xml file. I mean define them dynamically in code, but define them using the standar GridView API, then allow the button to size ITSELF WITHIN the gridview confines. – trumpetlicks Mar 31 '14 at 17:49
0

Pretty old post but if anyone get the same issue, here is the possible reason and solution:

You are probably setting the adapter and its list in the creation cycle of the fragment/activity (onCreate, onStart, etc..), therefore when the first view creation is done inside your adapter the GridView is not drawn yet and has a width of 0.

It does not happen to the other created views probably because the creation of the first one forced the GridView to be drawn.

You can find a bunch of solutions on that post: https://stackoverflow.com/a/24035591/3495069

I've personnaly ended up with the post method:

    mGridView.post(new Runnable() {
        @Override
        public void run() {
            mGridView.setAdapter(mAdapter);
        }
    });
Community
  • 1
  • 1
mVck
  • 2,910
  • 2
  • 18
  • 20
0

I had this same problems too. Here's what it'd looks like if it's your code.

if (convertView == null) {
    b = new Button(mContext);
    b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
    //b.setHeight(Game.gv.getMeasuredHeight() / Game.rows);
} else {
    b = (Button) convertView;
    if (b.getHeight() == 0)
        b.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / (Game.cols), parent.getHeight() / (Game.rows + 1)));
}
Nick
  • 1