Monty
Monty

Reputation: 3215

Remove selected item from GridView

Is there any way to remove selected item from gridview.

I want to delete the selected item from my gridview.

I did not find any thing . my code is

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

}

can anyone have idea . thank

Upvotes: 0

Views: 3467

Answers (2)

Raghu
Raghu

Reputation: 351

As Teovald and pskink suggested, you cannot have a fixed list of images and then implement the remove functionality that you are looking for. If you want to add remove, change your design and make sure your data set is also removable. What you have tried so far seems to be using some really basic code which is good to show basic GridView feature.

Just try this. Create a image data set class which returns the actual images. Store the images in a List that can be modified. Add setters/getters to this data set and also add a method to delete a particular item. Change your image adapter to use the image from this new data set. Whenever an image is deleted, call the notifyDataSetChanged on the adpater.

Good luck

Upvotes: 0

Teovald
Teovald

Reputation: 4389

you are using a table :

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}

Tables are not modifiable.
Replace it by a List on which you will be able to make add or remove operations. Simply call notifyDataSetChanged when a change is made to let the adapter know that its list has been modified.

Upvotes: 1

Related Questions