2013-07-23 17 views
0

你好stackoverflow我想開發一個應用程序,顯示SD卡的圖像,並允許用戶使用複選框刪除圖像。我可以使用CheckBox來顯示SD卡中的圖像,但我無法刪除用戶動態勾選的特定圖像。這裏是我的MainActivity.java如何使用Android中的按鈕在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; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getFromSdcard(); 
    GridView 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 

     } 
    }); 
} 

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

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

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

class ViewHolder { 
    ImageView imageview; 
} 

} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#FFFFFF" 
android:orientation="vertical" > 

<GridView 
    android:id="@+id/PhoneImageGrid" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="0.97" 
    android:columnWidth="90sp" 
    android:gravity="center" 
    android:horizontalSpacing="10sp" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="10sp" /> 

<RelativeLayout 
    android:id="@+id/rlBookmark" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:layout_gravity="bottom" > 

    <Button 
     android:id="@+id/btnBookmark" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="Clear" 
     android:textColor="#FF0011" 
     android:textSize="15sp" 
     android:textStyle="normal" /> 
</RelativeLayout> 

</LinearLayout> 

最後我galleryitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/thumbImage" 
    android:layout_width="100sp" 
    android:layout_height="100sp" /> 

<CheckBox 
    android:id="@+id/itemCheckBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" /> 

</RelativeLayout> 

請幫我解決這個謎,提前計算器感謝。

回答

0

stackoverflow最後我想出如何刪除在使用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 

    } 
}); 
} 

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

+0

這是工作正常,但在我的情況下,當我們刪除多個圖像,然後圖像將被刪除,但複選框dosent隱藏和imageview行。只有圖像路徑將從該方法中刪除。 – Google

+0

請你可以告訴我什麼是selectedFile和f這個語法if(!selectedFile.contains((String)f.get(pos)))。 – androidTag

+0

@Chetan Shetty:我也試圖實現刪除從攝像頭和圖庫中顯示的網格中選定的多個圖像。您可以幫我解決問題。 – androidTag

0

您可以參考這個例子Images with checkbox和刪除按鈕的點擊獲取圖像名稱並執行刪除這樣的操作:

File file= new File(Environment.getExternalStorageDirectory()+ "imageName"); 
if(file.exists()) 
{ 
    file.delete(); 
} 
+0

我怎樣才能刪除只檢查圖像? –

+0

你可以在這個例子中切爾滕他將布爾數組的值設置爲true或false基於複選框選擇並基於點擊循環通過arraylist獲取圖像名稱並通過該刪除功能 –

+1

獲取所選圖像的ID,然後獲取該ID的文件路徑。然後使用for循環,爲每個選定的id觸發delete命令。 – RAAAAM

相關問題