I'm trying to invisible an image when it is clicked by storing images from drawable into integer array using adapter list view, but I'm unable to get it. This is the code I'm using:
我试图通过使用适配器列表视图将图像从drawable存储到整数数组中来查看图像,但我无法得到它。这是我正在使用的代码:
When i click on an image it should get invisible.I am storing images in int array and applying setVisibilty invisible but its not working.i want an image to be displayed in centre of screen and the one which is clicked should get invisible.i am trying to store images in an integer array and setting it up in adapter list.i am calling this function
当我点击一个图像它应该是不可见的。我将图像存储在int数组中并应用setVisibilty不可见但它不工作。我想要一个图像显示在屏幕的中心,而点击的图像应该是隐形的。我是试图将图像存储在整数数组中并在适配器列表中设置它。我正在调用此函数
imageIDs[position].setVisible(false);
Integer[] imageIDs = {
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//Adapter list
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
//onclick event
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{//displaying image clicked i am trying to invisible this pic when click
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
Toast.LENGTH_SHORT).show();
//imageIDs[position].setVisible(false);
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
//setting image on screen from using xml
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//imageIDs[position].setVisible(false);
//i am trying it here but its not working
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
1 个解决方案
#1
I'm assuming you're trying to use this code:
我假设您正在尝试使用此代码:
//imageIDs[position].setVisible(false);
If so then what you're doing is calling setVisible on an Integer, which does not have that method. What you need to do is get a reference to the ImageView in which the image is being displayed and then call setVisibility(View.INVISIBLE) or setVisibility(View.GONE) on it.
如果是这样,那么你正在做的是在Integer上调用setVisible,它没有那个方法。您需要做的是获取对显示图像的ImageView的引用,然后在其上调用setVisibility(View.INVISIBLE)或setVisibility(View.GONE)。
Also it seems like you're trying to set the image to invisible but then you go and put the same resource back into the ImageView so I'm not sure what you're trying to do there.
此外,您似乎正在尝试将图像设置为不可见,但之后您将相同的资源放回ImageView中,因此我不确定您要在那里做什么。
#1
I'm assuming you're trying to use this code:
我假设您正在尝试使用此代码:
//imageIDs[position].setVisible(false);
If so then what you're doing is calling setVisible on an Integer, which does not have that method. What you need to do is get a reference to the ImageView in which the image is being displayed and then call setVisibility(View.INVISIBLE) or setVisibility(View.GONE) on it.
如果是这样,那么你正在做的是在Integer上调用setVisible,它没有那个方法。您需要做的是获取对显示图像的ImageView的引用,然后在其上调用setVisibility(View.INVISIBLE)或setVisibility(View.GONE)。
Also it seems like you're trying to set the image to invisible but then you go and put the same resource back into the ImageView so I'm not sure what you're trying to do there.
此外,您似乎正在尝试将图像设置为不可见,但之后您将相同的资源放回ImageView中,因此我不确定您要在那里做什么。