I currently have 2 classes named ProjectPreview and ImageAdapter.
Projectpreview has the view along with the actionbar.
I can’t figure out a way to get the ID of the current image within the imageview to the delete on the action bar.
oncreate in projectpreview:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
ImageAdapter ia1 = new ImageAdapter();
ia1.showLarger(position); }
Delete code in projectpreview:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
ImageAdapter class:
public Integer[] mImageIds = {
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
};
public void showLarger(int position){
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
image.setImageResource(mImageIds[position]);
}
I need to figure out a way to get the image.setImageResource(mImageIds[position]); to the other class so that I could remove the mImageIds[position] aka the picture.
To do this you can use the setTag function on your ImageView. The tag can any type of object you want such as the id used for the image resource. When you want to remove it you can use imageView.getTag() which will return you the object you had set with setTag. I see that your delete code is running when a menu item is clicked. I don’t see a reference to the ImageView there so you will need to run a findViewById to get a reference to the view.
Set the id:
Get the id: