选择时从Sd和Gridview中删除图像

时间:2014-06-23 16:57:24

标签: android gridview android-imageview android-gridview android-image

我有一个gridview应用程序,可以从SD卡加载图像。我想在选择项目时从SD卡中删除它并刷新网格视图。 这是我的代码:

主要

public class MainActivity extends Activity {
    private GridView gridView;
    private GridViewAdapter customGridAdapter;
    private String[] pathlist;
    private long code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_delta);
Log.w("!!!!!","!!!!");


        gridView = (GridView) findViewById(R.id.gridView);
        customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
        gridView.setColumnWidth(400);
        gridView.setPadding(8, 8, 8, 8);
        gridView.setAdapter(customGridAdapter);


        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                parent.getItemAtPosition(position);
                Toast.makeText(MainActivity.this, parent.getItemAtPosition(position) + "#Selected",
                        Toast.LENGTH_SHORT).show();
                //customGridAdapter.remove(position);
                //customGridAdapter.notifyDataSetChanged();

                for(int i=0;i<code;i++){

                    Log.w("path", pathlist[i].toString());
                }


            }
        }); 



    }





    private ArrayList<ImageItem> getData() {
        final ArrayList imageItems = new ArrayList();

        String stringpath = Environment.getExternalStorageDirectory().toString()+"/Pictures/Hello Camera";
        File path=new File(stringpath);
        File[] imageFiles = path.listFiles();
        for (int i = 0; i < imageFiles.length; i++) {
          //  Bitmap bitmap = BitmapFactory.decodeFile(imageFiles[i].getAbsolutePath());
          //  imageItems.add(new ImageItem(bitmap, "Image#" + i));
            code=imageFiles.length;
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;

            Bitmap bitmap = BitmapFactory.decodeFile(imageFiles[i].getAbsolutePath(),options);
            imageItems.add(new ImageItem(bitmap, "Image#" + imageFiles[i].getAbsolutePath().toString()));
          //  String cale=imageFiles[i].getAbsolutePath().toString();

        }

        return imageItems;}

}

适配器

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author javatechig {@link http://javatechig.com}
 * 
 */
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<ImageItem> data = new ArrayList<ImageItem>();

    public GridViewAdapter(Context context, int layoutResourceId,
            ArrayList<ImageItem> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.image = (ImageView) row.findViewById(R.id.image);
            //holder.checkbox = (CheckBox) row.findViewById(R.id.checkBox1);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        ImageItem item = data.get(position);
        holder.imageTitle.setText(item.getTitle());
        holder.image.setImageBitmap(item.getImage());

        //holder.checkbox.setChecked(false);

        return row;
    }



    static class ViewHolder {
        TextView imageTitle;
        ImageView image;
        CheckBox checkbox;
    }
}
你能告诉我怎么做吗? 谢谢!

0 个答案:

没有答案