title: android - Select Multiple Objects with checkboxes in custom GridView
body: <p>I'm developing a game and I'm having an Inventory as a GridView. Each grid item is a GameObject which is selected when clicked. The game indicates that the user can either select a single object or combine objects. </p>
  
  <p>What I need is very specific: I want when the user presses the menu button and the optionsMenu appears(with "combine" option), to dynamically draw a checkbox over each grid item, in order to multi-select some items (very much like in Gallery, but with objects instead of images) and then combine them.</p>
  
  <p>I'm using API level 8 so MultiChoiceMode is giving me a hard time.</p>
  
  <p>Also if anyone has a better suggestion on how to implement this functionallity rather than using checkboxes, I would surely be open to it. Thanx in advance</p>
  
  <p>Here's the xml for the grid items:</p>
  
  <pre><code>&lt;ImageView
      android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/&gt;
  
  &lt;CheckBox
      android:id="@+id/image_check"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:visibility="invisible" /&gt;
  </code></pre>
  
  <p>and the gridView:</p>
  
  <pre><code>&lt;TextView
      android:id="@+id/inventoryMsg"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:background="#000000"
      android:gravity="center"
      android:text="@string/inventory"/&gt;
  
  &lt;GridView
      android:id="@+id/gridView1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_above="@id/inventoryMsg"
      android:columnWidth="90dp"
      android:gravity="center"
      android:horizontalSpacing="10dp"
      android:numColumns="3"
      android:stretchMode="columnWidth"
      android:verticalSpacing="10dp" /&gt;
  </code></pre>
  
  <p>and this is the getViewMethod in my CustomAdapter:</p>
  
  <pre><code>public View getView (int position, View convertView, ViewGroup parent) {
  
  
              View v = convertView;
  
      if (convertView == null) {
          holder = new ViewHolder();
          LayoutInflater layoutInflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = layoutInflater.inflate(R.layout.griditem, null);
          holder.gameItemCheck = (CheckBox) v.findViewById(R.id.image_check);
          v.setTag(holder);
      } else {
          holder = (ViewHolder) v.getTag();
      }
      holder.gameItemCheck.setId(position);
      holder.gameItemCheck.setOnClickListener(new OnClickListener() {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
              //dosomestuff
          }
      });
  
      GameObject gameObject = gameObjectList.get(position);
      ImageView gameItemImageView = (ImageView) v.findViewById(R.id.grid_item_image);
  
  
      holder.gameItemCheck.setChecked(Inventory.selected[position]);
      holder.id = position;
      return v;
  }
  
  public class ViewHolder {
  
      public CheckBox gameItemCheck;
      int id;
  }
  </code></pre>
  
  <p><strong>The ways I've tried to change the checkboxes visibilty dynamicaly:</strong></p>
  
  <p>1)</p>
  
  <pre><code>public boolean onKeyDown(int keycode, KeyEvent e) {
      switch (keycode) {
      case KeyEvent.KEYCODE_MENU:
          openOptionsMenu();
          gameObjectAdapter.holder.gameItemCheck.setVisibility(View.VISIBLE);
          gridView.refreshDrawableState();
          return true;
      }
  
      return super.onKeyDown(keycode, e);
  }
  </code></pre>
  
  <p>2)</p>
  
  <pre><code>public boolean onPrepareOptionsMenu(Menu menu) {
              gameObjectAdapter.holder.gameItemCheck.setVisibility(View.VISIBLE);
              gameObjectAdapter.notifyDataSetChange();
      return true;
  
  }
  </code></pre>