在我的Android应用中,我有一个名为Gallery的活动,显示网格视图。每个网格都包含一张照片。我已经构建了单击处理程序,它生成一个带有OK按钮和ImageView的自定义警报对话框。我可以手动将ImageView源设置为可绘制资源(png文件)并正确显示,但我需要能够在自定义警报对话框中显示单击的图像(在我的网格视图中)。
这是我的代码:
包含OnCreate方法的activity_gallery:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
final ImageView clickedImage = new ImageView(this);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
loadPhoto(position);
}
});
}
我的loadPhoto方法从上面的代码中调用:
private void loadPhoto(int position) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.custom_fullimage_dialog, null);
ImageView image = (ImageView) view.findViewById(R.id.fullimage);
Resources resources = getResources();
image.setImageResource(R.drawable.webgallery_1);
imageDialog.setView(view);
imageDialog.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
imageDialog.create();
imageDialog.show();
}
正如您在上面的代码中所看到的,我可以将R.drawable.webgallery_1设置为照片的来源,没有任何问题。但无论点击什么照片,每次都会显示相同的图像。
我如何或如何从主代码传递到loadPhoto方法以识别被点击的照片?我可以传递位置,并返回在网格视图中单击的项的位置的int值,但我不确定如何使用它来将警报对话框中的图像资源设置为照片点击了。
答案 0 :(得分:0)
经过一番试验和错误,我得到了这个工作。它要求我对ImageAdapter类进行更改。请参阅以下更改:
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Object myObject = gridview.getItemAtPosition(position);
Integer myInt = ((Integer) myObject);
loadPhoto(position, myInt);
}
});
}
loadPhoto方法:
private void loadPhoto(int position, Integer myInt) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.custom_fullimage_dialog, null);
ImageView image = (ImageView) view.findViewById(R.id.fullimage);
Resources resources = getResources();
image.setImageResource(myInt);
imageDialog.setView(view);
imageDialog.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
imageDialog.create();
imageDialog.show();
}
}
ImageAdapter类
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public String id;
//private final String[] mobileValues;
String myString;
public ImageAdapter(Context c) {
mContext = c;
//String myList[] = c.getResources().toString();
//this.mobileValues = myList;
}
public int getCount() {
int myCount = 0;
Field[] fields = R.drawable.class.getFields();
for (Field f : fields) {
myString = f.getName();
String stub = myString.substring(0, 3);
if (stub.equals("web")) {
//String drawableName = "R.drawable." + myString;
int resID = ((Activity)mContext).getResources().getIdentifier(myString, "drawable", ((Activity)mContext).getPackageName());
array_image.add(resID);
myCount++;
}
}
return (myCount);
}
public Object getItem(int position) {
return array_image.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
DisplayMetrics dm = new DisplayMetrics();
((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int imageWidth = screenWidth / 2 - 50;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(imageWidth, imageWidth));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(array_image.get(position));
imageView.setTag(myString);
return imageView;
}
private ArrayList<Integer> array_image = new ArrayList<Integer>();
}