【问题标题】:Removing from ArrayList<Form> throws an UnsupportedOperationException从 ArrayList<Form> 中删除会引发 UnsupportedOperationException
【发布时间】:2012-04-29 19:17:03
【问题描述】:

我正在开发一个 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

【问题讨论】:

  • 对 Android SDK 不是很熟悉,但该列表是否实例化为不可修改列表?另外,你类中的“删除”方法有什么作用?
  • List 在传递给构造函数之前是如何实例化的?
  • @ExplodingRat 正确地询问了 List 的来源。 List 接口不要求可以删除项目,当不支持删除时会出现 UnsupportedOperationException。

标签: java android exception


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回答。有用。我还有另一个问题:我删除了第一个列表项(是列表中唯一检查的项目)。当我删除它时,第二个项目获得第一名并被检查。就像我必须做其他事情才能取消选中它。有什么线索吗?
  • 是的。在 getView() 中,您需要检查 convertview 的复选框是否被选中并相应地取消选中它
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2012-04-29
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多