0

I am newbie i want to remove the selected item from the spinner and also add the new item to the spinner.How can i do that.What i am trying is

adapter = ArrayAdapter.createFromResource(this,R.array.slot , android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            slotTime.setAdapter(adapter);
    slotTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectedTime = adapterView.getSelectedItem().toString();
                    adapter.remove((String) slotTime.getSelectedItem());
                    adapter.notifyDataSetChanged();

            }

I got the error like this....

     java.lang.UnsupportedOperationException

Anyone kindly help me to overcome this problem

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
mahesh
  • 11
  • 3
  • 1
    Possible duplicate of [Unable to modify ArrayAdapter in ListView: UnsupportedOperationException](https://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception) – vilpe89 Mar 20 '18 at 08:40
  • Just google "ArrayAdapter UnsupportedOperationException" and there you have it. It's not so hard nowadays. – vilpe89 Mar 20 '18 at 08:41

3 Answers3

0

your data is immutable (R.array.slot)

So you can modify it

You need to store data in List<String>, pass it to ArrayAdapter. Now you can modify data by modify List<String>

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
kemdo
  • 1,429
  • 3
  • 15
  • 29
0

When you pass an array in the ArrayAdapter class it converts the array into an AbstractList. This is an implementation of the List interface where elements cannot be added nor removed, i.e, immutable. What you should do is pass in an ArrayList by converting the array to a list. Arrays.asList method should do the trick.

Shababb Karim
  • 3,614
  • 1
  • 22
  • 35
0

used below code for remove spinner value and notify. here in strings as List.

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            strings.remove(arrstrings.indexOf(spinner.getSelectedItem()));
         //   spinner.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });