0

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

Monty
  • 3,205
  • 8
  • 36
  • 61

2 Answers2

1

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.

Teovald
  • 4,369
  • 4
  • 26
  • 45
  • this is taking only integer as a parameter imageView.setImageResource(mThumbIds[position]); – Monty May 06 '13 at 12:01
  • There are dozens of example showing you how to use an arraylist in a gridview/listview. Refer to one of them. Nothing prevent you for using an Arraylist of integers if it fits your usecase. – Teovald May 06 '13 at 12:13
0

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

Raghu
  • 351
  • 1
  • 4