Wednesday, 18 September 2013

How to put n intent on a gridview to lauch another activity in android?

How to put n intent on a gridview to lauch another activity in android?

I doing a grid view for my android layout but I don't know how to put an
intent on the grid view. is there any way where I can put an intent to
launch another activity? I been trying for few days already but i cant
seem to figure out how to do it.
public class PregnancyStages extends Activity implements
OnItemClickListener {
private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private ImageAdapter imageAdapter;
// Some items to add to the GRID
private static final String[] CONTENT = new String[] { "Pregnancy Stages",
"Complications", "Diet And Fitness", "Myths And Facts",
"FAQ's", "Helplines" };
private static final int[] ICONS = new int[] { R.drawable.baby1,
R.drawable.baby2,
R.drawable.baby3, R.drawable.baby4, R.drawable.baby5,
R.drawable.baby6 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the photo size and spacing
mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
mPhotoSpacing =
getResources().getDimensionPixelSize(R.dimen.photo_spacing);
// initialize image adapter
imageAdapter = new ImageAdapter();
photoGrid = (GridView) findViewById(R.id.albumGrid);
// set image adapter to the GridView
photoGrid.setAdapter(imageAdapter);
// get the view tree observer of the grid and set the height and
numcols dynamically
photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (imageAdapter.getNumColumns() == 0) {
final int numColumns = (int)
Math.floor(photoGrid.getWidth() / (mPhotoSize +
mPhotoSpacing));
if (numColumns > 0) {
final int columnWidth = (photoGrid.getWidth() /
numColumns) - mPhotoSpacing;
imageAdapter.setNumColumns(numColumns);
imageAdapter.setItemHeight(columnWidth);
}
}
}
});
}
// ///////// ImageAdapter class /////////////////
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private int mItemHeight = 0;
private int mNumColumns = 0;
private RelativeLayout.LayoutParams mImageViewLayoutParams;
public ImageAdapter() {
mInflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mImageViewLayoutParams = new
RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
}
public int getCount() {
return CONTENT.length;
}
// set numcols
public void setNumColumns(int numColumns) {
mNumColumns = numColumns;
}
public int getNumColumns() {
return mNumColumns;
}
// set photo item height
public void setItemHeight(int height) {
if (height == mItemHeight) {
return;
}
mItemHeight = height;
mImageViewLayoutParams = new
RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
mItemHeight);
notifyDataSetChanged();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
if (view == null)
view = mInflater.inflate(R.layout.photo_item, null);
ImageView cover = (ImageView) view.findViewById(R.id.cover);
TextView title = (TextView) view.findViewById(R.id.title);
cover.setLayoutParams(mImageViewLayoutParams);
// Check the height matches our calculated column width
if (cover.getLayoutParams().height != mItemHeight) {
cover.setLayoutParams(mImageViewLayoutParams);
}
cover.setImageResource(ICONS[position % ICONS.length]);
title.setText(CONTENT[position % CONTENT.length]);
return view;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long
id) {
// TODO Auto-generated method stub
Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(), PregnancyStagesGrid1.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(), PregnancyStagesGrid2.class);
}
startActivity(myIntent);
}}
anyone know where can i put this source code?
GridView gridview = (GridView) findViewById (R.id.albumGrid);
gridview.setAdapter (new ImageAdapter());
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int
position, long id) {
// TODO Auto-generated method stub
Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(),
PregnancyStagesGrid1.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(),
PregnancyStagesGrid2.class);
}
startActivity(myIntent);
}
});
}

No comments:

Post a Comment