-2

I have an gridview . The adapter of the gridview is as follows :

public class ImageAdapter extends ArrayAdapter<String> {
    private Context context;
    private final String[] mobileValues;

    public ImageAdapter(Context context,int resource, String[] mobileValues) {
        super(context, resource, mobileValues);
        this.context = context;
        this.mobileValues = mobileValues;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(mobileValues[position]);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            String mobile = mobileValues[position];

            if (mobile.equals("Windows")) {
                imageView.setImageResource(R.drawable.windows_logo);
            } else if (mobile.equals("iOS")) {
                imageView.setImageResource(R.drawable.ios_logo);
            } else if (mobile.equals("Blackberry")) {
                imageView.setImageResource(R.drawable.blackberry_logo);
            } else {
                imageView.setImageResource(R.drawable.android_logo);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

    @Override
    public String getItem(int position) {
        return mobileValues[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

I want to delete an item from the adapter by long click on the gridview . For this I have the following code :

public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        // TODO Auto-generated method stub
         imageAdapter.remove(imageAdapter.getItem(position));

        return false;
    }

But I get exception . How can I delete an item from adapterview on long click ?

osimer pothe
  • 2,827
  • 14
  • 54
  • 92
  • just remove item from `ArrayList` and then called `notifyDataSetChanged();` – M D Oct 25 '15 at 10:48
  • I have done it . see public boolean onItemLongClick(AdapterView> parent, View v, int position, long id) { // TODO Auto-generated method stub imageAdapter.remove(imageAdapter.getItem(position)); return false; } – osimer pothe Oct 25 '15 at 10:49
  • What is the correct way ? – osimer pothe Oct 25 '15 at 10:50
  • [http://stackoverflow.com/questions/17766218/android-remove-gridview-item-from-inside-getview](http://stackoverflow.com/questions/17766218/android-remove-gridview-item-from-inside-getview) – M D Oct 25 '15 at 10:51
  • [http://stackoverflow.com/questions/5306874/android-how-do-i-remove-an-item-from-a-gridview-having-the-remaing-items-shuff](http://stackoverflow.com/questions/5306874/android-how-do-i-remove-an-item-from-a-gridview-having-the-remaing-items-shuff) – M D Oct 25 '15 at 10:52
  • @osimerpothe: Best is use `List` instead of `Array` if want to perform remove or add operation in Adapter – ρяσѕρєя K Oct 25 '15 at 10:53

2 Answers2

1

Instead of using default remove method from ArrayAdapter for removing item from GridView. create a custom method and remove item from current data-source using position as:

public void removeItem(int pos){
 ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(mobileValues));
 arrayList.remove(pos);
 mobileValues = new String[arrayList.size()];
 arrayList.toArray(mobileValues);
 this.notifyDataSetChanged();
}

Now call removeItem in onItemLongClick method by passing position :

imageAdapter.removeItem(position);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

But I get exception.

In particular, the exception you are getting is

UnsupportedOperationException

The super constructor of ArrayAdapter that you are using is

public ArrayAdapter(Context context, int resource, T[] objects) {
        init(context, resource, 0, Arrays.asList(objects));
}

as you can notice, it uses Arrays.asList to transform your String[] in a List<String>, and the documentation of Arrays.asList states:

Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.

To workaround it, when you initialize your dataset, use an ArrayList<String>, or another concrete implementation of List. If you have already a String[], and you don't want to rewrite it, you can use new ArrayList<String>(Arrays.asList(yourArray))

Blackbelt
  • 156,034
  • 29
  • 297
  • 305