我创建了一个应用程序,其中图像显示在gridview上,我希望当我点击下一页全屏显示的特定图像时。
我已经写下了代码,但是当我点击图片时,它正在崩溃。
请有人帮助我
public class GridViewActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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) {
long imageId = (Long) parent.getAdapter().getItem(position);
Intent fullScreenIntent = new Intent(v.getContext(),FullScreenImage.class);
fullScreenIntent.putExtra("Hello",imageId);
startActivity(fullScreenIntent);
}
});
/* gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(GridViewActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});*/
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// 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;
}
imageView.setImageResource(images[position]);
return imageView;
}
// references to our images
private Integer[] images = {
R.drawable.bluehills, R.drawable.sample_3,
R.drawable.sample_7, R.drawable.sunset,
R.drawable.waterlilies, R.drawable.winter,
};
}
}
答案 0 :(得分:0)
请尝试此代码
package com.image;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ImageActivity extends Activity {
/** Called when the activity is first created. */
Integer[] mImageIds = { R.drawable.a, R.drawable.abc,
R.drawable.fd, R.drawable.ft, R.drawable.gh, R.drawable.nat
};
ImageView im;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery1);
im = (ImageView) findViewById(R.id.imageView1);
g.setAdapter(new ImageAdapter(this));
getWindow().addFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN );
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
Toast.makeText(ImageActivity.this, "" + position,Toast.LENGTH_SHORT).show();
im.setImageResource(mImageIds[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
i.bringToFront();
return i;
}
}
}