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:
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.
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:
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()));