First, here's an example of such an app for reference: http://www.themaninblue.com/experiment/JS-909/
So I created the layout using a GridView, bound to an ImageAdapter, which ties to an array of Boolean values (indicating if a particular switch is on or off). It creates the GridView correctly, but during debugging I noticed that the ID's of the Views get out of sync around 64, then for some reason start to change at random while the app is running ?????
I'm new to android/java, intermediate in c#/vb.net, so feel free to tear this code apart
Most of this is taken from the Grid2 tutorial:
Using java Syntax Highlighting
- public class ImageAdapter extends BaseAdapter {
- private Context mContext;
- public ImageAdapter(Context c) {
- mContext = c;
- }
- public int getCount() {
- return FunkyDrummer.trackSwitches.length;
- }
- // Gets the value for the specified array index
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(mContext);
- imageView.setLayoutParams(new GridView.LayoutParams(30, 40));
- imageView.setAdjustViewBounds(false);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- imageView.setPadding(4, 8, 4, <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />;
- // Set the ImageView's ID, I want to use 0-15 for row1, 16-31 for row 2, etc.
- imageView.setId((int)position);
- imageView.setOnClickListener(switchListener);
- }
- else{
- imageView = (ImageView) convertView;
- }
- // Set the ImageView's image based on it's active state
- if(FunkyDrummer.trackSwitches[position]==true){
- imageView.setImageResource(R.drawable.btn_on);
- }
- else{
- imageView.setImageResource(R.drawable.btn_off);
- }
- return imageView;
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
... and in the Activity constructor....
Using java Syntax Highlighting
- GridView g = (GridView) findViewById(R.id.grdMain);
- g.setNumColumns(16);
- g.setColumnWidth(30);
- g.setAdapter(new ImageAdapter(this));
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Honestly I'm kinda lost how it's determining how to bind to Boolean trackSwitches[] to begin with, but it seems to work. I have the OnClickHandler's to toggle the "button state" and a basic loop that triggers a SoundPool sound depending on the index at the time the timer loop hits, all in all it works fairly well, except for these random triggers who's View.getId() are out of order compared to the View to the left and right of it. Any ideas?


