按一次按钮从GridView中删除多个图像

时间:2013-05-07 10:43:09

标签: android gridview

我正在处理gridview,这是我要求在delete button按下一次从gridview删除多个所选图像项目。

表示我应该可以在删除按钮时删除所选图像。

我尝试了很多,但没有想到这个。

我可以一次删除一张图片。

这就是我所做的。

//活性

public class ImageActivity extends Activity {

    ImageAdapter iAdapter;
    ArrayList<Integer>myList;
    int position;
    GridView gView;
    boolean selected = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        dataImage();
        iAdapter = new ImageAdapter(this,myList);
        gView = (GridView)findViewById(R.id.grid_view);
        gView.setAdapter(iAdapter);
        gView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                gView.setSelection(position);
                gView.setSelected(true);
                Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_image, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.menu_delete)
        {
            if(gView.isSelected())
            {
                iAdapter.list.remove(iAdapter.getPosition());
                iAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Delete" ,Toast.LENGTH_SHORT ).show();
            }
        }
        gView.setSelected(false);
        return super.onOptionsItemSelected(item);
    }

    public void dataImage()
    {
        myList = new ArrayList<Integer>();
        myList.add(R.drawable.sample_1);
        myList.add(R.drawable.sample_3);
        myList.add(R.drawable.sample_2);
        myList.add(R.drawable.sample_2);
        myList.add(R.drawable.sample_1);
        myList.add(R.drawable.sample_3);
        myList.add(R.drawable.sample_1);
        myList.add(R.drawable.sample_3);
        myList.add(R.drawable.sample_2);


    }
}

//适配器

public class ImageAdapter extends BaseAdapter{

    Context context;
    ArrayList<Integer> list;
    int position;
    ImageView imageView;
    ImageActivity act;
    public ImageAdapter(Context context,ArrayList<Integer>list)
    {
        this.list = list;
        this.context = context;
        act = (ImageActivity)context;
        }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        this.position = position;
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 5, 0, 0);


        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(list.get(position));

        return imageView;
    }

    public int getPosition()
    {
        return position;

    }
}

//

任何人都可以指导我或帮助我。

我的要求 : - 如何一次性删除gridview中的多个图片按删除按钮。

2 个答案:

答案 0 :(得分:1)

您好我在CheckBox

中找出了如何使用GridView删除多张图片
public class MainActivity extends Activity {
    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private ImageAdapter imageAdapter;
    ArrayList<String> f = new ArrayList<String>();// list of file paths
    File[] listFile;

    Button btnDelete;

    private ProgressDialog pd;

    HashSet<String> selectedFile = new HashSet<String>();// list of file paths boolean checked

    GridView imagegrid;

    AlertDialog alertDialog = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getFromSdcard();
    imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);

    imageAdapter.notifyDataSetChanged();

    btnDelete = (Button) findViewById(R.id.btnDeleteImg);

        btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // to delete selected images
            Iterator iterator = selectedFile.iterator();
            while (iterator.hasNext()){
            new File(iterator.next().toString()).delete();
            }
           }
        });
    }

    public void getFromSdcard() {
        File file = new File("/mnt/sdcard/Images");

        if (file.isDirectory()) {
        listFile = file.listFiles();

        for (int i = 0; i < listFile.length; i++) {

               f.add(listFile[i].getAbsolutePath());

            }
        }
    }

    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return f.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
            holder.imageview.setImageBitmap(myBitmap);

            final int pos = position;

            holder.checkbox.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                if(!selectedFile.contains((String)f.get(pos)))
                {
                    selectedFile.add((String)f.get(pos));
                }
                else
                {
                    selectedFile.remove((String)f.get(pos));
                }
            }
        });
        return convertView;
    }
    }

    class ViewHolder {
        ImageView imageview;
        CheckBox checkbox;
        int id;
    }
}

这种方法对我有用,如果你们需要更多信息请发表评论,谢谢stackoverflow

答案 1 :(得分:0)

您可以使用自定义列表适配器,其中使用Check Box和ImageView(自定义视图)并在getView()中膨胀视图。在复选框中选择将其放入ArrayList并放入删除按钮,您可以通过该按钮删除列表中的所有图像,而不是通知适配器。