0

I want to remove data from a ListView. For that on a long press event I've used the code below:

      lstGame.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
            AlertDialog.Builder builder = new AlertDialog.Builder(FavouriteActivity.this);
            builder.setMessage("Remove from Favourite?").setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Const.favourite(FavouriteActivity.this, (args[arg2]));
                                Toast.makeText(FavouriteActivity.this, "Selected Item Removed from Favourite.", Toast.LENGTH_LONG).show();
                                // Here I get the UnsupportedException---->
                                // adapter.remove(args[arg2]);
                                lstGame.setAdapter(adapter);
                                lstGame.invalidate();

                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            Dialog alert = builder.create();
            alert.show();
            return false;
        }
    });

Why do I get that exception?

user
  • 86,916
  • 18
  • 197
  • 190
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

1 Answers1

1

If the adapter reference points to a default ArrayAdapter instance then you most likely instantiate the ArrayAdapter using an array of objects as its source of data. If this is the case then, under the hood, the ArrayAdapter will transform that array into a special ArrayList(not the normal java one). This special ArrayList doesn't implement the methods that change its size(so using methods like add or remove(which modify that list) on the ArrayAdapter will throw the UnsupportedOperationException), it will only allow you to modify the values in it.

If you want to use that remove method then put the data from the array that you currently use in the ArrayAdapter in an ArrayList and then pass that list to the ArrayAdapter constructor.

user
  • 86,916
  • 18
  • 197
  • 190