Here is my code:
1st activity::
public class ImageExercise extends Activity {
/** Called when the activity is first created. */
public static int pos;
//static int[] mThumbIds;
//public static int[] mThumbIds;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(ImageExercise.this,FullImage.class);
pos=position;
intent.putExtra("pos", pos);
startActivity(intent);
//finish();
}
});
}
}
class ImageAdapter extends BaseAdapter {
static int[] mThumbIds = {
R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_8,
R开发者_JAVA百科.drawable.sample_9,R.drawable.sample_1,
R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_9,
R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7, R.drawable.sample_8
};
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
/* ImageExercise obj = new ImageExercise();
obj.pos = position;*/
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
}
2nd Activity::
package com.ex.dp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
Bundle bundle= getIntent().getExtras();
ImageView image = (ImageView) findViewById(R.id.imageView1);
int pos = bundle.getInt("pos");
ImageAdapter obj = new ImageAdapter(this);
image.setImageResource(obj.mThumbIds[pos]);
/*if(pos==0)
{
image.setImageResource(pos);
}*/
}
}
Plz help me out
uncomment the line // super.onCreate(savedInstanceState);
in onCreate
You have to call onCreate()
of super class everytime you create an Activity.
in your 2nd activity, uncomment the line super.onCreate(savedInstanceState);
As @Saurabh and @Tushar just said
精彩评论