为什么 ArrayAdapter 中的原始数组项也会被删除?

我通过适配器的 add() 和 clear() 从适配器对象中添加和删除值,它们正在删除元素并将其添加到列表中,该列表不会从适配器内的任何位置引用。我有原始列表,它位于适配器外部,并具有从适配器内部到不更改外部引用的引用,而在适配器内部,我仅引用一个新列表,我使用旧的原始列表填充了相同的对象。新名单为何会影响旧名单?


class LanguageItemArrayAdapter extends ArrayAdapter<com.anysoftkeyboard.ui.settings.LanguageItem>{

    private Context mContext;

    private final ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem> origList = new ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem>();

    private final ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem> filteredList = new ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem>();


    LanguageItemArrayAdapter(@NonNull Context context, @LayoutRes ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem> list, ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem> filteredList){

        super(context, 0, filteredList);

        mContext = context;

        origList.addAll(list); //THIS WORKS WELL. 

        //THIS BREAKS EVERYTHING -> 

        //origlist = list;

    }


    public void fillData(){

        filteredList.addAll(origList);

        notifyDataSetChanged();


    }


    @Override

    public View getView(int position,  View convertView, @NonNull ViewGroup parent) {

        View listItem = convertView;

        if (listItem == null)

            listItem = LayoutInflater.from(mContext).inflate(R.layout.languages_list_row, parent, false);

        com.anysoftkeyboard.ui.settings.LanguageItem currentItem = filteredList.get(position);


        TextView title = listItem.findViewById(R.id.title);

        title.setText(currentItem.getTitle());


        return listItem;

    }

为什么原始列表会被编辑,而我只更改过滤后的列表?


侃侃无极
浏览 67回答 1
1回答

aluckdog

此代码将相同的实例传递ArrayList给LanguageItemArrayAdapternew LanguageItemArrayAdapter(getContext(), simpleLanguageItems, simpleLanguageItems)要理解为什么add(…)和clear()in也修改同一个对象,您需要从源代码ArrayAdapter中查看它们的实现。public void clear() {&nbsp; &nbsp; synchronized (mLock) {&nbsp; &nbsp; &nbsp; &nbsp; if (mOriginalValues != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mOriginalValues.clear();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mObjects.clear();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; …&nbsp; &nbsp; }&nbsp; &nbsp; …}clear()将修改mObjects和mOriginalValues(您会注意到其他函数也ArrayAdapter做同样的事情)。您需要阅读里面的代码ArrayFilter以了解它们是如何修改的。private class ArrayFilter extends Filter {&nbsp; /*&nbsp;&nbsp; mObjects will contain only items fulfilling the filter conditions.&nbsp;&nbsp; Original items are copied into mOriginalValues&nbsp;&nbsp; */}查看构造函数(和构造函数链),您会发现您的类构造函数有super(context, 0, filteredList);最终会调用private ArrayAdapter(@NonNull Context context,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @LayoutRes int resource,&nbsp; &nbsp; &nbsp; &nbsp; @IdRes int textViewResourceId,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @NonNull List<T> objects,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; boolean objsFromResources) {&nbsp; &nbsp; …&nbsp; &nbsp; mObjects = objects;&nbsp; &nbsp; …}这允许ArrayAdapter修改您的simpleLanguageItems实例。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java