0

私は取り組んでgridviewいます.1回のdelete buttonプレスでグリッドビューから複数の選択した画像アイテムを削除するという要件があります。

削除ボタンを押すと、選択した画像を削除できるはずです。

私はたくさん試しましたが、これについてはわかりませんでした。

一度に 1 つの画像を削除できます。

これは私がしたことです。

//アクティビティ

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削除ボタンを押します。

4

2 に答える 2

1

こんにちは、で使用して複数の画像を削除する方法を見つけましCheckBoxGridView

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

于 2013-07-23T10:31:56.747 に答える