0

Possible Duplicate:
Create a clickable image in a GridView in Android

I have set up a grid layout but want to make the images open separate activity's my code looks like this:

package android.grid.layout;

import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter { private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.group_off, R.drawable.indoor_off, R.drawable.outdoors_off,
        R.drawable.attractions_off, R.drawable.playcentre_off, R.drawable.animals_nature_off,
        R.drawable.entertainment_off, R.drawable.arts_off, R.drawable.educational_off, 
        R.drawable.museum_off, R.drawable.historical_off, R.drawable.exercise_off,
        R.drawable.swimming_off, R.drawable.restaurant_off, R.drawable.partyhat_off
};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    return imageView;
}

}

and the class that reads it looks like this

package android.grid.layout;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView;

public class HomeActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

}   

}

How would i make each one click-able to open a new separate activity to have custom lists inside?

Community
  • 1
  • 1
Cem22lp
  • 113
  • 6
  • have you seen [this](http://stackoverflow.com/questions/738817/create-a-clickable-image-in-a-gridview-in-android)? – keyser May 23 '12 at 09:12

1 Answers1

0

Inside Adpater's GetView add a click listener,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
   imageView.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View view) {
   Intent i=new Intent(context,activity.class);
     startActivity(i);
  }

});

    return imageView;
}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Sorry how do i get that to link to each image to make each one start its own different activity? – Cem22lp May 23 '12 at 09:24
  • Seriously, you want to start different activities? How are you gonna do that. Are you going to create Activities dynamically? No Way. You might have to consider using Put Extra of Intents and use a flag value and do your operations in a single activity. – Andro Selva May 23 '12 at 09:28
  • If not there are two methods. Inside onClick of the image you wil be able to get the position, so based on the position hardcode the Activity to be started., Or declare a Activity array and array and based on the position call that activity form that array. But I dont believe it is possible simply – Andro Selva May 23 '12 at 09:29