2013-05-07 66 views
0

我正在對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在同一時間按下按鈕刪除刪除多張圖像。

回答

0

您可以使用自定義列表適配器,其中使用複選框和ImageView(自定義視圖)並在getView()中充氣視圖。在複選框中選擇將其放入ArrayList中,並將刪除按鈕,您可以從列表中刪除所有圖像,並通知適配器。

+0

對於任何樣品... – Unknown 2013-05-07 11:36:55

1

您好我想出如何在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; 
    } 
} 

這種方法爲我工作刪除使用CheckBox多張圖片,如果你們需要更多的信息,請發表評論,謝謝計算器

+0

我也嘗試刪除多個選定的圖像,但有一段時間我的應用程序崩潰並得到java.lang.IndexOutOfBoundsException:索引1無效,大小爲1錯誤 – androidTag 2015-10-21 04:46:24

+0

那麼你的數組/集合的大小是1,你是試圖刪除索引高於數組/集合大小的項目。刪除循環完成後,您必須保持刷新視圖。如果您可以共享源代碼,那麼修復該錯誤將更容易。 – 2015-10-21 07:16:54

+0

感謝您的回覆。我有我的代碼在這裏發佈= http://stackoverflow.com/questions/33251664/android-indexoutofbounds-exception-while-delete-multiple-images-from-gridview?noredirect=1#comment54305913_33251664 – androidTag 2015-10-21 07:18:24