title: Android : Update Image in GridView on touch event in Adapter
body: <p>I have a <code>GridView</code> and attached adapter to it. Adapter populates images in the grid. I have set <code>setOnTouchLister</code> to <code>GridView</code> in my <code>Activity</code> and implemented in the adapter only. In adapter I have an <code>Integer[] imageIDs</code> that contains resource ids of all images that are added &amp; an <code>ArrayList&lt;ImageSourceObject&gt; imgObjsArr</code> that extends <code>ImageView</code> &amp; has other properties set.</p>
  
  <p>Now <code>onTouch()</code>, I want to change the image of the selected image to other one. Here's my code :
  SEtting adapter of grid in <code>Activity</code> in <code>onCreate</code> :</p>
  
  <pre><code>        // Set Objects in Game View
      gameView = (GridView) this.findViewById(R.id.game_gridView);
      gameObjAdapter = new GameObjectsAdapter(this, R.id.game_gridView, null);
      gameView.setAdapter(gameObjAdapter);
  </code></pre>
  
  <p>ADAPTER :</p>
  
  <pre><code>public class GameObjectsAdapter extends BaseAdapter implements OnTouchListener {
      super();
      mContext = c;
      this.layoutResourceId = layoutResourceId;
      objects = data;
      gridViewResAdap = (GridView) mContext.findViewById(this.layoutResourceId);
      createObjectsArray();
      Log.d("GOA", "Created Objects Array");
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      ImageSourceObject imgView;
  
      if (convertView == null) {
          imgView = new ImageSourceObject(mContext);
          imgView.setLayoutParams(new GridView.LayoutParams(20, 20));
          imgView.getScaleType();
          imgView.setScaleType(ScaleType.FIT_XY);
          imgView.setPadding(0, 0, 0, 0);
      } else {
          imgView = (ImageSourceObject) convertView;
      }
  
      // Chk status of touched of imgView &amp; set image accordingly
      if (imgView.isTouched())
          imgView.setImage(R.drawable.droid_touched2);
      else 
          imgView.setImage(imageIds[position]);
  
      return imgView;
  }   
  
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      // TODO Auto-generated method stub
  
      int action = event.getActionMasked();
      float currentXPos = event.getX();
      float currentYPos = event.getY();
      int position = gridViewResAdap.pointToPosition((int)currentXPos, (int) currentYPos);
  
      // Key was Pressed Here
      if (action == MotionEvent.ACTION_UP) {
          if (position &gt; 0) {
              // Get the object which is clicked
              ImageSourceObject isb = this.imgObjsArr.get(position);
              Log.d("GA", " Postion ID " + isb.getId() + " [] ID : " + imageIds[position]);
  
              // Change the status of touched &amp; set image
              isb.setTouched(true);
              isb.setImage(R.drawable.droid_touched2); 
  
              // Update the ArrayList &amp; Integer[] with this updated obj 
              this.imgObjsArr.set(position, isb);
              imageIds[position] = R.drawable.droid_touched2;
              Log.d("ISB", "++++ Object ID : " + isb.getId() + " [] ID : " + imageIds[position] + " ISB Touched :" + isb.isTouched());
  
              Toast.makeText(mContext, "Postion Pressed : " + (position+1), 
                  Toast.LENGTH_SHORT).show();
  
              //this.gridViewResAdap.invalidate();
          }
  
          return true;
      }
  
      return false;
  }
  </code></pre>
  
  <p>Logs :</p>
  
  <pre><code>02-19 15:19:11.615: D/GA(2046):  Postion ID 2130837556 [] ID : 2130837556
  02-19 15:19:11.617: D/ISB(2046): ++++ Object ID : 2130837559 [] ID : 2130837559 ISB Touched :true
  </code></pre>
  
  <p>The logs say that the object in <code>Integer[]</code> &amp; <code>ArrayList</code> both are updated &amp; have right values. After all this also the image is not updated on the screen. The <code>gridViewResAdap</code> is also the object of grid that is passed from the activity. I tried calling <code>invalidate()</code> on it also, but yet no results. As in my <code>getView()</code>, I am using <code>imageIDs</code>, so I have kept that also updated. </p>
  
  <p>ImageSourceObject :</p>
  
  <pre><code>public class ImageSourceObject extends ImageView {
  
  public void setImage(int resourceId) {
      super.setImageResource(resourceId);
      this.setId(resourceId);
  }
  </code></pre>
  
  <p>Also, <code>onTouch()</code> gives error to call <code>onPerformClick()</code>, I am not sure where to call &amp; why to call. I have alos not implemented <code>onClickListener</code> in the adapter. What can be done in this case &amp; what to write in <code>onClickListener</code> when things are manged in <code>onTouch()</code>.</p>
  
  <p>Can you help me know why the image object is not updating and where am I going wrong ? I thought I have to refresh the grid, so have also called invalidate(), but no results. </p>
  
  <p>Any help is highly appreciated.</p>
  
  <p>Thanks</p>