title: Change image when clicking on item in a GridView
body: <p>I have a GridView with 9 items. What I'm trying to do is when I click an item, its icon changes. The way I'm doing it returns </p>
  
  <pre><code>java.lang.ClassCastException: android.widget.LinearLayout
  </code></pre>
  
  <p>I have to classes:</p>
  
  <p><strong>MainLayout.java</strong></p>
  
  <pre><code>public class MainLayout extends Activity implements OnItemClickListener
  {   
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          requestWindowFeature(Window.FEATURE_NO_TITLE);
  
          setContentView(R.layout.mainlayout);
  
          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);      
  
           final ImageButton btn=(ImageButton)findViewById(R.id.logoimagebutton);
           btn.setBackgroundColor(Color.TRANSPARENT);
  
           btn.setOnTouchListener(new OnTouchListener() {
  
                  public boolean onTouch(View v, MotionEvent event) {
                      if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                          btn.setImageResource(R.drawable.logoselected);
                          Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
                          //return true;
                      }else if (event.getAction() == MotionEvent.ACTION_UP) {
                          btn.setImageResource(R.drawable.logo);
                      }
  
                      return false;
                  }
              });
  
          final GridView gridview = (GridView) findViewById(R.id.gridview);
          gridview.setAdapter(new ImageAdapter(this));            
  
          gridview.setOnItemClickListener(new OnItemClickListener()
          {
              public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id)
              {                   
                  ImageView imageView = (ImageView) v;
                  imageView.setImageResource(ImageAdapter.mThumbSelected[position]);
              }
          });   
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3)
      {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p><strong>ImageAdapter.java</strong></p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter
  {
  
      private Context mContext;
  
      public static Integer[] mThumbIds = 
      {
              //a collection of images
      };
  
      public static Integer[] mThumbSelected = 
      {
              //a collection of images
      };
  
      private String[] mLabelsIds = {//a collection of strings};
  
      public ImageAdapter(Context c)
      {
          // TODO Auto-generated constructor stub
          mContext = c;
      }
  
      @Override
      public int getCount()
      {
          // TODO Auto-generated method stub
          return mThumbIds.length;
      }
  
      @Override
      public Object getItem(int arg0)
      {
          // TODO Auto-generated method stub
          return mThumbIds[arg0];
      }
  
      @Override
      public long getItemId(int arg0)
      {
          // TODO Auto-generated method stub
          return arg0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {       
          View grid;
  
          if (convertView == null)
          {
              grid = new View(mContext);
              LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              grid = inflater.inflate(R.layout.menugrid, parent, false);
          } else
          {
              grid = (View) convertView;
          }
  
          ImageView imageView = (ImageView) grid.findViewById(R.id.imageicon);
          TextView textView = (TextView) grid.findViewById(R.id.imagelabel);
          imageView.setImageResource(mThumbIds[position]);
          textView.setText(mLabelsIds[position]);
  
          return grid;
      }
  }
  </code></pre>
  
  <p>Here is my two xml files:</p>
  
  <p><strong>menugrid.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"&gt;
  
       &lt;ImageView
        android:id="@+id/imageicon"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        /&gt;
       &lt;TextView
        android:id="@+id/imagelabel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"     
        android:textColor="#000000"
        android:textSize="16px"
        android:gravity="center_horizontal"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>mainlayout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mainlayout"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" 
      android:orientation="vertical"
      android:background="@drawable/background"&gt;
  
      &lt;LinearLayout
          android:id="@+id/holdlogo"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"    
          android:orientation="horizontal"                    
          android:background="@drawable/title_background"&gt;
  
          &lt;ImageButton
              android:id="@+id/logoimagebutton"
              android:scaleType="center"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"    
              android:layout_gravity="center_vertical"    
              android:src="@drawable/logo"
              &gt;
          &lt;/ImageButton&gt;
  
      &lt;/LinearLayout&gt;
  
  
      &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/gridview"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
      android:numColumns="auto_fit"
      android:layout_marginTop="10px"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:columnWidth="90dp"
      android:stretchMode="columnWidth"
      android:gravity="center"
  /&gt;  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Can anyone tell me what is wrong in my code? Or How to change the image of an item when pressing it? I think onTouchListener is more efficient, due to the fact that it has the event, so when <code>event.getAction() == MotionEvent.ACTION_DOWN</code> I show the image and when <code>event.getAction() == MotionEvent.ACTION_UP</code> I show the original icon.</p>
  
  <p>Thanks in advance.</p>