0

我已经实现了我的代码来删除和更改长按网格视图的视图。这是我更新的代码。现在我长按时遇到问题然后我必须更改所有项目图像(我在长按时向所有人添加更多视图 - 即十字按钮)

在单击该十字按钮时,我想删除该项目。请帮我。

 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;
            }



    }
4

1 回答 1

2

需要从底层适配器的数据集中移除对应的对象(mThumbIds),然后调用adapter.notifyDataSetChanged()

您可以将数据存储在 Vector 中,例如:

Vector<Integer> mThumbIds = new Vector<Integer>();

像这样初始化它:

for (int i = 0; i < mThumbIds.size(); i++) {
            mThumbIds.add(R.drawable.ic_launcher);
        }

removeitem()

public void removeitem(int position){
    mThumbIds.remove(position);

    }

然后删除..

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();
            adapter.removeitem(position);
            adapter.notifyDataSetChanged();
            return false;

        }
于 2012-10-01T09:50:22.287 回答