0

I'm developing an Android 3.1 application.

I have created my custom ArrayAdapter with an ArrayList. Form is a custom class with two fields: name and FormId.

Here is my ArrayAdapter code:

public class FormAdapter extends ArrayAdapter<Form>
{
    private Context context;
    private int layoutResourceId;
    private List<Form> forms;
    private ArrayList<Integer> checkedItemsPosition;
    private Button downloadButton;

    public ArrayList<Integer> getCheckedItemsPosition()
    {
        return checkedItemsPosition;
    }

    public String[] getSelectedFormsId()
    {
        String[] ids = new String[checkedItemsPosition.size()];
        int i = 0;
        for(Integer pos : checkedItemsPosition)
        {
            Form f = forms.get(pos.intValue());
            ids[i] = f.FormId;
            i++;
        }
        return ids;
    }

    /**
     * Called when selected forms has been downloaded and save it locally correctly.
     */
    public void updateFormsNotDownloaded()
    {
        for (Integer pos : checkedItemsPosition)
        {
            remove(forms.get(pos.intValue()));
        }

        checkedItemsPosition.clear();
        notifyDataSetChanged();
    }

    public FormAdapter(Context context, int textViewResourceId,
            List<Form> objects, Button downloadButton)
    {
        super(context, textViewResourceId, objects);

        this.context = context;
        this.layoutResourceId = textViewResourceId;
        this.forms = objects;
        this.checkedItemsPosition = new ArrayList<Integer>();
        this.downloadButton = downloadButton;
    }

    @Override
    public int getCount()
    {
        return forms.size();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        Log.v("FormAdapter", "getView.postion: " + position);
        View row = convertView;
        if (row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
        }

        Form f = forms.get(position);
        if (f != null)
        {
            CheckBox checkBox = (CheckBox)row.findViewById(R.id.itemCheckBox);
            if (checkBox != null)
            {
                checkBox.setText(f.Name);
                checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
                {
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked)
                    {
                        //Form f = forms.get(position);
                        if (isChecked)
                        {
                            //checkedItems.add(f.FormId);
                            checkedItemsPosition.add(new Integer(position));
                        }
                        else
                        {
                            //checkedItems.remove(checkedItems.indexOf(f.FormId));
                            checkedItemsPosition.remove(checkedItemsPosition.indexOf(new Integer(position)));
                        }
                        downloadButton.setEnabled(checkedItemsPosition.size() > 0);
                    }
                });
            }
        }

        return row;
    }
}

List items are custom items with a checkbox. On checkedItemsPosition I store checked items position.

My problem is on updateFormsNotDownloaded method. Why am I getting an UnsupportedOperationException?

Gerstmann
  • 5,368
  • 6
  • 37
  • 57
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • Not very familiar with the Android SDK but is that list instantiated as an unmodifiableList? Also, what does the method "remove" in your class do? – Apoorv Apr 18 '12 at 11:27
  • http://stackoverflow.com/questions/3476723/why-cant-one-add-remove-items-from-an-arrayadapter – assylias Apr 18 '12 at 11:29
  • How's the List instantiated before it's passed to the constructor? – Gerstmann Apr 18 '12 at 11:29
  • 1
    @ExplodingRat is right to ask where the List comes from. The List interface doesn't require that items can be removed, and you get the UnsupportedOperationException when removal is not supported. – David Snabel-Caunt Apr 18 '12 at 11:48

1 Answers1

1

I can only think of one reason. The List<> implementation you pass into the ArrayAdapters constructor does not support remove(). You can use an ArrayList to fix that. If for some reason you are using Arrays.asList() to construct your list from an array you are getting a list which cannot be modified.

The size of the
     * {@code List} cannot be modified, i.e. adding and removing are unsupported
Renard
  • 6,909
  • 2
  • 27
  • 23
  • Thanks for your answer. It works. I have another problem: I remove first list item (is the only item checked on list). When I remove it, second item gets a first place and the it gets checked. It is like I have to do something else to uncheck it. Any clue? – VansFannel Apr 18 '12 at 11:58
  • yes. In getView() you need to check if the checkbox of convertview is checked and uncheck it accordingly – Renard Apr 18 '12 at 12:08