I am trying unsuccessfully to add a custom button to my layout. The custom button is defined in a layout xml called tile_quarter.xml. It has a imageview and 6 testviews. How can i add this to the gridview?
My efforts to date plus code. I have a gridview and a layoutadapter which is meant to add a custom button to my gridview dynamically. I cant get it to work, no errors showing just a blank screen. The button is there as when i tap it the toast appears as coded but none of its controls visible.
Layout adapter
public class LayoutAdapter extends BaseAdapter {
private Context mContext;
public LayoutAdapter(Context c) {
mContext = c;
}
public int getCount() {
return tileIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public RelativeLayout getView(int position, View convertView, ViewGroup parent) {
RelativeLayout layoutView;
if (convertView == null) { // if it's not recycled, initialize some attributes
layoutView = new RelativeLayout(mContext);
layoutView.setLayoutParams(new GridView.LayoutParams(500, 500));
layoutView.setPadding(8, 8, 8, 8);
} else {
layoutView = (RelativeLayout) convertView;
}
layoutView.findViewById(tileIds[position]);
//.setImageResource(mThumbIds[position]);
return layoutView;
}
// references to our images
private Integer[] tileIds = {
R.layout.tile_quarter
};
}
Main Class
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new LayoutAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
0 comments:
Post a Comment