从GridView中删除所选项目

时间:2013-05-06 11:05:15

标签: android gridview android-gridview

有没有办法从gridview删除所选项目。

我想从item删除所选的gridview

我没找到任何东西。我的代码是

public class ImageAdapter extends BaseAdapter{

    Context context;

    public ImageAdapter(Context context)
    {
        this.context = context;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return  mThumbIds.length;
    }

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

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

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

        final ImageView imageView;
        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(mThumbIds[position]);
        return imageView;
    }

    public Integer[] mThumbIds = {
            R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
            R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1

    };

}

//////////////////

public class ImageActivity extends Activity {

    ImageAdapter iAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
         iAdapter = new ImageAdapter(this);
        final GridView 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.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                // gView.setItemChecked(position, true);
                 Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                }

        });
        iAdapter.notifyDataSetChanged();
    }

    @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)
        {
            Toast.makeText(this, "Delete",Toast.LENGTH_SHORT ).show();
        }
        return super.onOptionsItemSelected(item);
    }

}

任何人都可以有想法。 感谢

2 个答案:

答案 0 :(得分:1)

你正在使用一张桌子:

public Integer[] mThumbIds = {
        R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
        R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
        R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1}

表格不可修改 将其替换为您可以在其上进行添加或删除操作的列表。当进行更改以让适配器知道其列表已被修改时,只需调用notifyDataSetChanged。

答案 1 :(得分:0)

建议Teovaldpskink,您不能拥有固定的图片列表,然后实现您要查找的删除功能。如果您要添加删除功能,请更改您的设计并确保您的数据集也可移动。你到目前为止所尝试的似乎是使用一些非常基本的代码,这对于显示基本的GridView功能很有用。

试试吧。创建一个返回实际图像的图像数据集类。将图像存储在可以修改的列表中。将setter / getter添加到此数据集,还添加删除特定项的方法。更改图像适配器以使用此新数据集中的图像。每当删除图像时,请调用adpater上的 notifyDataSetChanged

祝你好运