从ArrayList <form>中删除会抛出UnsupportedOperationException </form>

时间:2012-04-18 11:25:53

标签: java android exception

我正在开发Android 3.1应用程序。

我使用ArrayList创建了自定义ArrayAdapter。 Form是一个自定义类,包含两个字段:name和FormId。

这是我的ArrayAdapter代码:

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;
    }
}

列表项是带有复选框的自定义项。在checkedItemsPosition我存储已检查的项目位置。

我的问题出在updateFormsNotDownloaded方法上。为什么我会收到UnsupportedOperationException

1 个答案:

答案 0 :(得分:1)

我只能想到一个原因。列表&lt;&gt;传递给ArrayAdapters构造函数的实现不支持remove()。您可以使用ArrayList来修复它。 如果由于某种原因您使用Arrays.asList()从数组构造列表,您将获得一个无法修改的列表。

The size of the
     * {@code List} cannot be modified, i.e. adding and removing are unsupported