gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:28:27 25 4
gpt4 key购买 nike

我通过适配器的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;
}

private Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem> tempList=new ArrayList<com.anysoftkeyboard.ui.settings.LanguageItem>();
//constraint is the result from text you want to filter against.
//objects is your data set you will filter from
if(constraint != null) {
int length= origList.size();
int i=0;
while(i<length){
LanguageItem item= origList.get(i);
if(item.toString().toUpperCase().contains(constraint.toString().toUpperCase())) {
tempList.add(item);
}
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = tempList;
filterResults.count = tempList.size();
}

return filterResults;
}

//HERE ARE THE AFFECTING METHODS:
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList.clear();

clear(); //******THIS METHOD REMOVES THE VALUES from origList as well******
filteredList.addAll((Collection<? extends LanguageItem>) results.values);
notifyDataSetChanged();

//*****THIS ADDS VALUES TO ORIG LIST AS WELL. WHY?*****
for(LanguageItem item: filteredList)
add(item);

}
};

@Override
public Filter getFilter() {
return myFilter;
}
}

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

编辑:

ArrayAdapter<com.anysoftkeyboard.ui.settings.LanguageItem> adapter = new LanguageItemArrayAdapter(getContext(), simpleLanguageItems, simpleLanguageItems);
((LanguageItemArrayAdapter) adapter).fillData();;

(我知道修复方法,但我不明白原因 - 如果您像我一样遇到自定义适配器过滤器的问题,则修复方法位于下面的代码中。)

最佳答案

此代码将 ArrayList 的相同实例传递给 LanguageItemArrayAdapter

new LanguageItemArrayAdapter(getContext(), simpleLanguageItems, simpleLanguageItems)

要理解为什么 ArrayAdapter 中的 add(…)clear() 也会修改同一个对象,您需要查看它们的实现source .

public void clear() {
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.clear();
} else {
mObjects.clear();
}

}

}

clear() 将修改 mObjectsmOriginalValues (您会注意到 ArrayAdapter 中的其他函数执行以下操作:相同的)。您需要阅读 ArrayFilter 内的代码以了解它们是如何修改的。

private class ArrayFilter extends Filter {
/*
mObjects will contain only items fulfilling the filter conditions.
Original items are copied into mOriginalValues
*/
}

查看构造函数(和构造函数链),您会发现您的类构造函数具有

super(context, 0, filteredList);

最终会调用

private ArrayAdapter(@NonNull Context context, 
@LayoutRes int resource,
@IdRes int textViewResourceId,
@NonNull List<T> objects,
boolean objsFromResources) {

mObjects = objects;

}

这允许ArrayAdapter修改您的simpleLanguageItems实例。

关于java - 为什么 ArrayAdapter 中的原始数组项也会被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com