グリッドビューを長押しするとビューを削除および変更するコードを実装しました。これが私の更新されたコードです。今、私は長押しすると問題が発生し、すべてのアイテムの画像を変更する必要があります(長押しですべてにビューを追加しています-つまり、クロスボタン)
そのクロスボタンをクリックすると、そのアイテムを削除したいと思います。私を助けてください。
public class Deletable extends Activity
{
private LayoutInflater inflater = null;
Boolean checkLong = false;
Vector<Integer> mThumbsID = new Vector<Integer>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView gridview = (GridView) findViewById(R.id.gridview);
final ImageAdapter adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mThumbsID.setSize(9);
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View strings,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(Deletable.this, "Deletable "+ position, Toast.LENGTH_SHORT).show();
checkLong = true;
//adapter.removeitem(position);
adapter.notifyDataSetChanged();
return false;
}
});
/*gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
checkLong = false;
adapter.notifyDataSetChanged();
}
});*/
}
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return mThumbsID.size();
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
public void removeitem(int position){
mThumbsID.remove(position);
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder = new ViewHolder();
if (vi == null) {
vi = inflater.inflate(R.layout.custom_gridview, null);
}
holder.productImage = (ImageView)vi.findViewById(R.id.gv_item);
holder.delete_icon = (ImageView)vi.findViewById(R.id.imageView1);
holder.productImage.setImageResource(R.drawable.ic_launcher);
if(checkLong==true){
holder.delete_icon.setVisibility(View.VISIBLE);
holder.delete_icon.setImageResource(R.drawable.cross);
/* holder.delete_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mThumbsID.remove(position);
}
});
*/
}
else{
holder.delete_icon.setVisibility(View.INVISIBLE);
}
/*if (convertView == null)
{
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(R.drawable.ic_launcher);*/
return vi;
}
class ViewHolder
{
//TextView tvBrandName;
ImageView productImage,delete_icon;
}
}