我已经使用自定义适配器实现了gridview,并且适配器使布局膨胀并在gridview的每个元素中显示两个项目。一个是imageview,另一个是关闭按钮,它位于图像的右上方。我的问题是当我点击十字按钮时从网格中删除元素。我要为两张图片触发事件。所以我计划在适配器内给出关闭按钮事件。最后它可以工作,但我无法删除点击的位置。每当我点击关闭按钮图像时,它将从我的arraylist中删除最后一个元素。请帮助我。
我的代码在下面:
@Override
public View getView( int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
pos=position;
positionForCheck =(Object) pos;
if (convertView == null)
{
LayoutInflater li = (LayoutInflater) MyContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MyView = li.inflate(R.layout.grid_item_for_bag, null);
// Add The Image!!!
ImageView imageSrc = (ImageView) MyView.findViewById(R.id.grid_item_bag_image);
ImageView imageClose = (ImageView) MyView.findViewById(R.id.grid_item_bag_close);
}
imageSrc.setImageResource(MyList.get(position));
imageClose.setImageResource(R.drawable.closeicon_31x31);
imageClose.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
MyList.remove(position)
notifyDataSetChanged();
}
});
return MyView;
}
答案 0 :(得分:0)
我得到了输出。 代码如下所示
@Override
public View getView( int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
ViewHolder holder;
pos=position;
positionForCheck =(Object) pos;
if (convertView == null)
{
LayoutInflater li = (LayoutInflater) MyContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MyView = li.inflate(R.layout.grid_item_for_bag, null);
holder = new ViewHolder();
// Add The Image!!!
holder.imageSrc = (ImageView) MyView.findViewById(R.id.grid_item_bag_image);
holder.imageClose = (ImageView) MyView.findViewById(R.id.grid_item_bag_close);
MyView.setTag(holder);
}
else
{
holder =(ViewHolder) MyView.getTag();
}
holder.imageSrc.setImageResource(MyList.get(position));
holder.imageClose.setTag(position)
holder.imageClose.setImageResource(R.drawable.closeicon_31x31);
holder.imageClose.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int posi = Integer.parseInt(v.getTag().toString());
MyList.remove(posi)
notifyDataSetChanged();
}
});
return MyView;
}