我通过适配器的 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;
}
为什么原始列表会被编辑,而我只更改过滤后的列表?
aluckdog
相关分类